是否可以在bash中运行for循环来迭代多个目标?

时间:2013-03-27 15:09:54

标签: bash for-loop

是否可以在bash中运行for循环来迭代多个目标?或者我是否需要求助于嵌套for循环?

例如:

for i in *.config,*.command; do
  echo "Do something"
done

感谢。

3 个答案:

答案 0 :(得分:2)

你是说这个意思吗?你想要实现的目标还不是很清楚。你能说明你想用哪些配置文件或参数运行哪些命令吗?

for i in *.config *.command; do
  echo "Do something to $i"
done

(注意:没有逗号!)

答案 1 :(得分:0)

让文件globbing在数组声明中扩展。

$ touch {1..5}.txt
$ touch {1..5}.cfg

# use nullglob
$ shopt -s nullglob

$ list=(*.txt *.cfg)

# loop over all elements in array
$ for file in "${list[@]}"; do echo "$file"; done
1.txt
2.txt
3.txt
4.txt
5.txt
1.cfg
2.cfg
3.cfg
4.cfg
5.cfg

# number of items
$ echo "${#list[@]}"
10

请参阅nullglob

答案 2 :(得分:-1)

你可以使用

for i in `ls *.config *.command` 
do
  echo "hhhh"
done
相关问题