linux 当中需要反复执行某一条命令或一组命令时,需要用到循化结构化命令,循环命令用于特定条件下决定某些语句重复执行的控制方式,shell中的循环语句常用三种循环语句,分别是for循环,while循环,until循环。
1.for 循环
列表for 循环:用于将一组命令执行已知的次数,do 和done 之间为循环体。
使用for循环,显示3个“你好“的操作。
[root@foundation77 mnt]# cat test.sh #编辑脚本,in 后接3个参数。
#!/bin/bash
for varible1 in a b c
do
echo "你好"
done
[root@foundation77 mnt]# sh test.sh #由于有三个参数,便输出3个“你好”
你好
你好
你好
使用for 循环计算1-100 之间的奇数
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
sum=0
for i in {1..100..2} #范围为1-100 ,跳跃2
do
let "sum+=i"
done
echo "sum=$sum"
[root@foundation77 mnt]# sh test.sh
sum=2500
使用for 循环打印九九乘法表
[root@foundation77 mnt]# vim test.sh
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
for i in $(seq 9)
do
for j in $(seq $i)
do
echo -ne "$i*$j=$(($i*$j))\t"
done
echo -e "\n" #-e 表示转译,\n 表示换行。
done
[root@foundation77 mnt]# sh test.sh
1*1=1
2*1=2 2*2=4
3*1=3 3*2=6 3*3=9
4*1=4 4*2=8 4*3=12 4*4=16
5*1=5 5*2=10 5*3=15 5*4=20 5*5=25
6*1=6 6*2=12 6*3=18 6*4=24 6*5=30 6*6=36
7*1=7 7*2=14 7*3=21 7*4=28 7*5=35 7*6=42 7*7=49
8*1=8 8*2=16 8*3=24 8*4=32 8*5=40 8*6=48 8*7=56 8*8=64
9*1=9 9*2=18 9*3=27 9*4=36 9*5=45 9*6=54 9*7=63 9*8=72 9*9=81
2.while 循环
while循环也成为前测试循环,如果满足循环则执行循环体,否则自动退出。
使用while批量添加用户
[root@foundation77 mnt]# vim test.sh
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
PREFIX="linux"
i=1
while [ $i -le 20 ]
do
useradd -r ${PREFIX}$i &> /dev/null
echo "123456" | passwd --stdin ${PREFIX}$i &> /dev/null
((i++))
done
[root@foundation77 mnt]# sh test.sh
[root@foundation77 mnt]# tail -20 /etc/passwd
linux1:x:988:983::/home/linux1:/bin/bash
linux2:x:987:982::/home/linux2:/bin/bash
linux3:x:986:981::/home/linux3:/bin/bash
linux4:x:985:980::/home/linux4:/bin/bash
linux5:x:984:979::/home/linux5:/bin/bash
linux6:x:983:978::/home/linux6:/bin/bash
linux7:x:982:977::/home/linux7:/bin/bash
linux8:x:981:976::/home/linux8:/bin/bash
linux9:x:980:975::/home/linux9:/bin/bash
linux10:x:979:974::/home/linux10:/bin/bash
linux11:x:978:973::/home/linux11:/bin/bash
linux12:x:977:972::/home/linux12:/bin/bash
linux13:x:976:971::/home/linux13:/bin/bash
linux14:x:975:970::/home/linux14:/bin/bash
linux15:x:974:969::/home/linux15:/bin/bash
linux16:x:973:968::/home/linux16:/bin/bash
linux17:x:972:967::/home/linux17:/bin/bash
linux18:x:971:966::/home/linux18:/bin/bash
linux19:x:970:965::/home/linux19:/bin/bash
linux20:x:969:964::/home/linux20:/bin/bash
3.if 语句:它时条件语句的一种格式,针对某种条件作出相应处理。
简单if 语句
[root@foundation77 mnt]# cat number.sh
#!/bin/bash
user=yao
if grep $user /etc/passwd;then #grep 搜索变量$user 为yao 的内容存在的话,输出 hello yao
echo "Hello $user"
fi
[root@foundation77 mnt]# sh number.sh
yao:x:1001:1001::/home/yao:/bin/bash
Hello yao
if /else 语句与嵌套:简单if结构只能满足表达式时才可以,if /else 是双向语句,具有较好交互性,也可以支持三个或以上的嵌套。
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
user=kiosk #设置变量为kiosk
if grep $user /etc/passwd;then #如果kiosk在/etc/passwd 中存在
echo "The files for user $user are:" #执行以下两条命令
ls -a /home/$user
else #否则,执行此命令
echo "$user not exist!"
fi
[root@foundation77 mnt]# sh test.sh
kiosk:x:1000:1000:kiosk:/home/kiosk:/bin/bash
The files for user kiosk are:
. .bash_profile Documents .gnome2 .lesshst Pictures Videos
.. .bashrc Downloads .gnome2_private .local Public .viminfo
.adobe .cache .esd_auth .gnupg .macromedia .ssh .vnc
.bash_history .config .fltk .ICEauthority .mozilla Templates
.bash_logout Desktop .gimp-2.8 .kingsoft Music .thumbnails
[root@foundation77 mnt]#
if //elif/else 结构 :此结构比if/else 结构的嵌套简单,且可读性高。
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
if [ "$1" == "root" ];then #如果变量为root时,执行下一条命令
echo "Welcome $1"
elif [ "$1" == "westos" ];then #如果是westos,执行下一条命令
echo "Welcome $1"
elif [ "$1" == "kiosk" ];then
echo "Welcome $1"
elif [ "$1" == "yao" ];then
echo "Welcome $1"
else #如果都不满足,输出“not allowd”
echo "not allowed!"
fi
[root@foundation77 mnt]# sh test.sh yao
Welcome yao
[root@foundation77 mnt]# sh test.sh root
Welcome root
[root@foundation77 mnt]# sh test.sh linux1
not allowed!
4. case 结构
case 结构同样可以用于多分支选择语句,
[root@foundation77 mnt]# cat test.sh
#!/bin/bash
case $1 in # 如果输入的变量是以下四个中的一个
student|kiosk|linux|westos)
echo "Welcome,$1" #输出 Welcome,$1
;;
*)
echo "Sorry!" #不存在的话输出 sorry!
;;
esac
[root@foundation77 mnt]# sh test.sh root
Sorry!
[root@foundation77 mnt]# sh test.sh kiosk
Welcome,kiosk
[root@foundation77 mnt]# sh test.sh linux
Welcome,linux
[root@foundation77 mnt]#