gpt4 book ai didi

shell - 计算 shell 变量中的项目数

转载 作者:行者123 更新时间:2023-12-04 19:51:34 25 4
gpt4 key购买 nike

我有一个 shell 变量 sampleDir:

sampleDir=/home/sample*
echo $sampleDir

将给予:

/home/sample_001 /home/sample_002 /home/sample_003

(注意sample_***下有子目录)

如果我想计算这个 $sampleDir 变量中有多少这些项目,请问最好的方法是什么? (预计返回 3 个)

最佳答案

将变量存储在数组中:

sampleDir=(/home/sample*)

使用参数扩展获取数组中的项目数:

echo "${#sampleDir[@]}"

关于shell - 计算 shell 变量中的项目数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44934225/

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