linux shell脚本中流程控制语句 if、for、while、case
1、if语句
[root@centos7 test2]# ls test.sh [root@centos7 test2]# pwd/home/test2 [root@centos7 test2]# cat test.sh#!/bin/bash DIR="/home/test2/test3/test4"if [ ! -e $DIR ] then mkdir -p $DIR echo "new file" > $DIR/new.file fi[root@centos7 test2]# bash test.sh [root@centos7 test2]# ls test3 test.sh [root@centos7 test2]# tree . ├── test3 │ └── test4 │ └── new.file └── test.sh2 directories, 2 files [root@centos7 test2]# cat ./test3/test4/new.filenew file
2、if
[root@centos7 test2]# ls a.txt test.sh [root@centos7 test2]# cat a.txt12345[root@centos7 test2]# cat test.sh #!/bin/bash num=$(wc -l a.txt | awk '{print $1}') tail=$(tail -n 1 a.txt)if [ $num -eq 5 ] && [ $tail -eq 5 ] then echo "right!"elseecho "error!"fi [root@centos7 test2]# bash test.sh right![root@centos7 test2]# echo "6" >> a.txt [root@centos7 test2]# cat a.txt123456[root@centos7 test2]# bash test.sh error!
3、if
[root@centos7 test2]# ls test.sh [root@centos7 test2]# cat test.sh #!/bin/bash ping -c 3 -w 6 $1 &> /dev/nullif [ $? -eq 0 ] then echo "$1 is online!"elseecho "$1 is offline!"fi [root@centos7 test2]# bash test.sh 192.168.3.59192.168.3.59 is online![root@centos7 test2]# bash test.sh 192.168.3.70192.168.3.70 is offline![root@centos7 test2]# bash test.sh www.baidu.com www.baidu.com is online!
4、if
[root@centos7 test2]# ls test.sh [root@centos7 test2]# cat test.sh #!/bin/bash read -p "please input your score: " SCOREif [ $SCORE -ge 90 ] && [ $SCORE -le 100 ] then echo "excellent!"elif [ $SCORE -ge 60 ] && [ $SCORE -lt 90 ] then echo "pass!"elif [ $SCORE -ge 0 ] && [ $SCORE -lt 60 ] then echo "failure!"elseecho "out of the range! the range is 0-100"fi [root@centos7 test2]# bash test.sh please input your score: 97excellent![root@centos7 test2]# bash test.sh please input your score: 86pass![root@centos7 test2]# bash test.sh please input your score: 34failure![root@centos7 test2]# bash test.sh please input your score: 342out of the range! the range is 0-100[root@centos7 test2]# bash test.sh please input your score: -342out of the range! the range is 0-100
5、for语句
[root@centos7 test2]# cat a.txt abcde02 abcde03 abcde04 abcde05 abcde06 abcde07 [root@centos7 test2]# cat test.sh #!/bin/bash read -p "please input the passwd: " PASSWDfor i in `cat a.txt`doid $i &> /dev/nullif [ $? -eq 0 ] then echo "$i had existed!"elseuseradd $i &> /dev/nullecho $PASSWD | passwd --stdin $i &> /dev/nullif [ $? -eq 0 ] then echo "$i had created successfully!"elseecho "$i created failed!"fi fi done [root@centos7 test2]# bash test.sh please input the passwd: abc123456 abcde02 had created successfully!abcde03 had created successfully!abcde04 had created successfully!abcde05 had created successfully!abcde06 had created successfully!abcde07 had created successfully![root@centos7 test2]# userdel -r abcde03 [root@centos7 test2]# userdel -r abcde04 [root@centos7 test2]# userdel -r abcde05 [root@centos7 test2]# bash test.sh please input the passwd: abc123456 abcde02 had existed!abcde03 had created successfully!abcde04 had created successfully!abcde05 had created successfully!abcde06 had existed!abcde07 had existed![root@centos7 test2]# for i in `cat a.txt`; do userdel -r $i; done
6、for语句
[root@centos7 test2]# ls a.txt test.sh [root@centos7 test2]# cat a.txt www.baidu.com www.xxxxxxyyy.com www.taobao.com www.jjjjjjxxxxx.com www.jd.com www.tencent.com11111111[root@centos7 test2]# cat test.sh #!/bin/bashfor i in `cat a.txt`doping -c 3 -w 6 $i &> /dev/nullif [ $? -eq 0 ] then echo "$i is online!"elseecho "$i is offline!"fi done [root@centos7 test2]# bash test.sh www.baidu.com is online!www.xxxxxxyyy.com is offline!www.taobao.com is online!www.jjjjjjxxxxx.com is offline!www.jd.com is online!www.tencent.com is online!11111111 is offline!