for 变量名 in 取值列表 do 命令序列 done
[root@localhost ~]# files=`ls` [root@localhost ~]# echo $files abc.txt addrs.txt anaconda-ks.cfg dead.letter demo.sh first.sh func.sh guess.sh ifcfg-ens33 initial-setup-ks.cfg myping.sh res.txt useradd.sh user.txt us.txt 公共 模板 视频 图片 文档 下载 音乐 桌面 [root@localhost ~]# for x in $files > do > echo $x > done abc.txt addrs.txt anaconda-ks.cfg dead.letter demo.sh first.sh func.sh guess.sh ifcfg-ens33 initial-setup-ks.cfg myping.sh res.txt useradd.sh user.txt us.txt 公共 模板 视频 图片 文档 下载 音乐 桌面
[root@localhost ~]# for (( e=1;e<=10;e++ )) > do > echo $e > done 1 2 3 4 5 6 7 8 9 10
[root@localhost ~]# for (( e=2;e<=10;e=$[e+2] )) > do > echo $e > done 2 4 6 8 10
[root@localhost ~]# vim user.txt [root@localhost ~]# us=`cat user.txt` [root@localhost ~]# echo $us zhangsan lisi wanqwu [root@localhost ~]# for name in $us > do > useradd -m -p 123456 $name > done
#!/bin/bash addrs=`cat /root/addrs.txt` for addr in $addrs do res=`ping $addr -c 1 -W 1 | grep "packet loss" | awk -F, '{print $3}'` echo $addr $res >> res.txt done
while 条件测试操作 do 命令序列 done
#!/bin/bash a=1 while [ $a -le 20 ] do useradd stu$a echo 123456 | passwd --stdin stu$a let a++ done
#!/bin/bash a=0 b=0 price=$(echo $[$RANDOM % 1000]) while [ $a -eq 0 ] do read -p "please enter a number:" num if [ $num -eq $price ];then let b++ echo "Bingo!You tried $b times." let a++ elif [ $num -gt $price ];then let b++ echo "Too large,please try again" elif [ $num -lt $price ];then let b++ echo "Too small,please try again" fi done
until 条件测试操作 do 命令序列 done
计算1~50的和值
#!/bin/bash i=0 s=0 until [ $i -eq 51 ];do let s+=i;let i++ done echo $s