我正在创建一个脚本,该脚本列出目录中的所有nano文件。汉比文件添加了一个数字。(为每个文件添加+1)。然后,允许用户查看nano文件。
到目前为止,这就是我所拥有的。我想指出的是,所有文件名都以“。”结尾,_log
这就是为什么我希望grep像这样工作。
path=~/home/folder/list
list=$(`ls $path | grep -i \*_log`)
printf '%s\n' "${list[@]}" | nl -v 1
read -p "Number of file to be displayed:" numb
sudo cat $path/${list [numb]}
如果我理解正确,那么您想创建一个包含所有文件的数组,然后显示与用户输入的编号相对应的任何文件的内容。如果是这样,您会使事情变得比必要复杂得多。这应该足够了:
## Get the files into the array $list
list=(/home/folder/list/*_log)
## Display the file names
for i in ${!list[@]}; do
printf "%s: %s\n" $i "${list[i]}";
done
## Get the user input
read -p "Number of file to be displayed:" numb
## display the file (don't use sudo unless absolutely necessary)
cat "${list[numb]}"
请注意,这将显示文件名,包括整个路径。要仅显示名称,请将for
循环更改为:
## Display the file names
for i in ${!list[@]}; do
printf "%s: %s\n" $i "${list[i]##*/}"
done
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句