我有一个变量$ x有一个样本值,如isd21anms.com,isd31anms.com等。现在我想迭代for循环将1号从1改为1,2,3 4.请看下面的代码
for i in 1 2 3 4
do
change $i such that I get values isd21anms.com, isd22anms.com, isd23anms.com, isd24anms.com
done
记住$ x可以有像isd21anms.com,isd31anms.com,isd41anms.com这样的值。所以我必须将它保存在变量中并在其上应用循环而不是使用常量值。
答案 0 :(得分:1)
我会使用大括号扩展
for i in isd{1..4}{1..4}anms.com; do
echo $i
done
输出:
isd11anms.com
isd12anms.com
isd13anms.com
isd14anms.com
isd21anms.com
isd22anms.com
isd23anms.com
isd24anms.com
isd31anms.com
isd32anms.com
isd33anms.com
isd34anms.com
isd41anms.com
isd42anms.com
isd43anms.com
isd44anms.com
答案 1 :(得分:0)
如果我理解你的问题:
for x in isd21anms.com isd31anms.com isd41anms.com
do
for i in {1..4}
do
y=${x/1/$i}
echo $y
done
done
在运行时,得到了:
isd21anms.com
isd22anms.com
isd23anms.com
isd24anms.com
isd31anms.com
isd32anms.com
............
答案 2 :(得分:0)
for i in 1 2 3 4
do
y=`echo $temp | sed "s/\(.*\).\(anms.com\)/\1"${i}"\2/g"`
echo "y is $y"
done
测试如下:
> ./temp.sh
y is isd21anms.com
y is isd22anms.com
y is isd23anms.com
y is isd24anms.com
> setenv temp "isd31anms.com"
> ./temp.sh
y is isd31anms.com
y is isd32anms.com
y is isd33anms.com
y is isd34anms.com