简体   繁体   English

计算shell脚本中由find函数找到的文件,并将它们保存在数组中

[英]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.

相关问题 如何在Java脚本中查找和计数字符串中的元音并以数组形式返回它们? - How to find and count vowels in string and return them in array in Java Script? Shell脚本-查找作为参数传递给函数的数组的长度 - Shell Script - Find length of an array passed as parameter to a function Python脚本查找目录中所有文件的文件名和大小并将其保存到文本文件以进行excel导入 - Python Script to find file name and size of all files in a directory and save them to a text file for excel import Shell脚本:如何将循环输出保存到数组 - Shell script: How to save loop output to array 将数据保存到SQL并根据json数组计数显示它们 - save data to SQL and also display them according to json array count 查找文件递归并保存在动态数组(VBA)中 - find files recursive and save in dymanic array (VBA) 尝试向数组添加值时出现“+=:找不到命令” - Shell 脚本 - '+=: Command not found' when trying to add a value to an array - Shell Script Javascript / ES6 查找唯一元素并在数组中计算它们 - Javascript / ES6 find unique elements and count them in array 从数组中查找总和为 X 的项目的最小数量 - Find minimum count of items where sum of them is X from an array 计数函数在PHP中不起作用以查找数组的大小 - Count function not working in PHP to find size of array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM