0

是否可以在 bash 中运行一个 for 循环来遍历多个目标?还是我需要使用嵌套的 for 循环?

例如:

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

谢谢。

4

3 回答 3

2

你的意思是这样吗?目前还不太清楚你想要达到什么目标。你能说出你想用哪些配置文件或参数运行哪些命令吗?

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

(注意:没有逗号!)

于 2013-03-27T15:15:26.663 回答
0

让文件通配符在数组声明中展开。

$ 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

空球

于 2013-03-27T15:22:13.863 回答
-1

您可以使用

for i in `ls *.config *.command` 
do
  echo "hhhh"
done
于 2013-03-27T15:14:55.487 回答