[英]count files found by find function in a shell script and save them in an array
I would like to make a shell script that finds a specific file and then asks the user which file he wants. 我想制作一个Shell脚本,该脚本查找特定文件,然后询问用户他想要哪个文件。 But when I for example use the find command
但是当我使用例如find命令时
(find . -name 2D.py)
it finds two files: 它找到两个文件:
./Desktop/2D.py ./libcpab-master/2D.py
save these into a file: 将它们保存到文件中:
file=$(find . -name 2D.py)
and counts them: 并计算它们:
echo "${#key[@]}"
I get that only one name. 我只有那个名字。 It should be 2 files.
它应该是2个文件。 Also when I save 'file' in a array, there is only one element.
另外,当我将“文件”保存在数组中时,只有一个元素。
Hope you can help. 希望能对您有所帮助。
You have to define the "file" variable as an array. 您必须将“文件”变量定义为数组。 Try this
尝试这个
> find . -name 2D.py
./Desktop/2D.py
./libcpab-master/2D.py
> set -A file
> file=$(find . -name 2D.py)
> echo "${file[*]}"
./Desktop/2D.py
./libcpab-master/2D.py
> echo "${file[*]}" > file_list
> cat file_list
./Desktop/2D.py
./libcpab-master/2D.py
> wc file_list
2 2 39 file_list
>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.