我需要一个目录中的文件总数,并想在 shell 脚本中使用这个数字。 我在终端试过这个,它工作正常:
find . -type f | wc -l
它只是打印文件的数量,但我想将返回的数字分配给我的 shell 脚本中的一个变量,我试过了,但它不起作用:
numberOfFiles = find . -type f | wc -l;
echo $numberOfFiles;
最佳答案
要存储命令的输出,需要使用var=$(command)
语法:
numberOfFiles=$(find . -type f | wc -l)
echo "$numberOfFiles"
当前方法存在的问题:
numberOfFiles = find . -type f | wc -l;
^ ^
| space after the = sign
space after the name of the variable
no indication about what are you doing. You need $() to execute the command
您当前正在尝试使用以下参数执行 numberOfFiles
命令:= find 。 -类型f | wc -l;
,这显然不是你想要做的:)
关于linux - 将目录中的文件数返回到 shell 脚本中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27942749/