gpt4 book ai didi

linux - 将目录中的文件数返回到 shell 脚本中的变量

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:17:33 24 4
gpt4 key购买 nike

我需要一个目录中的文件总数,并想在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com