循环语句 for

For 变量 in 字符串
do
语句 1
done

案例一,打印 seq 数字循环

#!/bin/sh
for i in `seq 15`
do
echo “NUM is $i”
done

案例二,求和 1-100 的值

#!/bin/bash
#auto sum 1 100
j=0
for ((i=1;i<=100;i++))
do
 j=`expr $i + $j`
done
echo $j

案例三,找到相关 log,然后批量打包

#!/bin/sh
for i in `find /var/log -name “*.log”`
do
tar –czf 2014log.tgz $i
done

案例四,远程主机批量传输文件:

#!/bin/bash
#auto scp files for client
#by authors wugk 2014
for i in `seq 100 200`
do
 scp -r /tmp/test.txt
root@192.168.1.$i:/data/webapps/www
Done

案例五,远程主机批量执行命令:

#!/bin/bash
#auto scp files for client
#by authors wugk 2014
for i in `seq 100 200`
do
 ssh -l root 192.168.1.$i ‘ls /tmp’
done

案例六,顺序输出当前列表中的数字:

~~~bash
for loop in 1 2 3 4 5
do
    echo "The value is: $loop"
done
~~~