Linux 将目录中的文件数返回到shell scrtpt中的变量
我需要目录中的总文件数,并希望在shell脚本中使用此数字。 我在航站楼试过,效果很好:Linux 将目录中的文件数返回到shell scrtpt中的变量,linux,shell,ubuntu,Linux,Shell,Ubuntu,我需要目录中的总文件数,并希望在shell脚本中使用此数字。 我在航站楼试过,效果很好: find . -type f | wc -l 它只是打印文件的数量,但我想将返回的数量分配给shell脚本中的一个变量,我尝试了这个方法,但不起作用: numberOfFiles = find . -type f | wc -l; echo $numberOfFiles; 要存储命令的输出,需要使用var=$(命令)语法: numberOfFiles=$(find . -type f | wc -l)
find . -type f | wc -l
它只是打印文件的数量,但我想将返回的数量分配给shell脚本中的一个变量,我尝试了这个方法,但不起作用:
numberOfFiles = find . -type f | wc -l;
echo $numberOfFiles;
要存储命令的输出,需要使用
var=$(命令)
语法:
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型代码>,这显然不是您想要做的:)将命令输出分配给需要使用的变量时,请尝试此操作。或者您也可以使用$(命令)
。两者都是正确的方式
numberOfFiles=`find . -type f | wc -l`;
echo $numberOfFiles;
反勾号非常过时,首选$()
,因为您可以嵌套它们。比如说,是的,我知道,但有时我还是忍不住要用背虱,因为自从学习以来我用了很多时间,这已经成为一种习惯。谢谢你的链接@fedorqui,它提供了很多信息。