Shell编程进阶 1.8 for循环

产生序列的命令

seq 1 10

1 2 3 4 5 6 7 8 9 10

seq 1 2 10

1 3 5 7 9  (从1开始增加2显示这个数字,到10结束)

seq 10 -2 1

10 8 6 4 2 

seq 10 -1 1

10 9 8 7 6 5 4 3 2 1 

seq -w 1 10

01 02 03 04 05 06 07 08 09 10 

seq -w 1 100

001 002 003 004 005 006 ....098 099 100

 

循环1-10

vim for.sh
#!/bin/bash
for i in `seq 1 10`
do
        echo $i
done
sh for.sh

1 2 3 4 5 6 7 8 9 10 

#!/bin/bash
for i in {1..10}
do
        echo $i
done

效果一样

 

计算1-10的和

vim for1.sh
#!/bin/bash
sum=0
for i in {1..10}
do
        sum=$[$sum+$i]
done
        echo $sum

指定sum等于0

$i=1-10

sum=0+(1-10)

结束循环

输出新的$sum

 

循环文件的内容

vim 1.txt
1 2 3
qqqqqqq
aa bb
for l in `cat 1.txt`;do echo $l;done

1
2
3
qqqqqqq
aa
bb

 

 

vim 3.txt
1.1.1.1
2.2.2.2
3.3.3.3
for ip in `cat 3.txt`;do echo $ip;ssh $ip "w";done
for file in `ls`;do echo $file;du -sh $file;done

 

posted @ 2016-01-02 13:32  dennyLinux  阅读(272)  评论(0编辑  收藏  举报