Return 目录中的文件数到 shell scrtpt 中的变量
Return number of files in a directory to a variable in a shell scrtpt
我需要一个目录中的文件总数,并想在 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 . -type f | wc -l;
,这显然不是您想要的:)
试试这个,在将命令输出分配给需要使用 ` 的变量时。或者您也可以使用 $(command)
。两种方法都对。
numberOfFiles=`find . -type f | wc -l`;
echo $numberOfFiles;
我需要一个目录中的文件总数,并想在 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 . -type f | wc -l;
,这显然不是您想要的:)
试试这个,在将命令输出分配给需要使用 ` 的变量时。或者您也可以使用 $(command)
。两种方法都对。
numberOfFiles=`find . -type f | wc -l`;
echo $numberOfFiles;