linux shell脚本中流程控制语句 if 、for、while、case

 

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.sh

2 directories, 2 files
[root@centos7 test2]# cat ./test3/test4/new.file
new file

 

2、if

[root@centos7 test2]# ls
a.txt  test.sh
[root@centos7 test2]# cat a.txt
1
2
3
4
5
[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!"
else
echo "error!"
fi
[root@centos7 test2]# bash test.sh
right!
[root@centos7 test2]# echo "6" >> a.txt
[root@centos7 test2]# cat a.txt
1
2
3
4
5
6
[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/null
if [ $? -eq 0 ]
then
echo "$1 is online!"
else
echo "$1 is offline!"
fi
[root@centos7 test2]# bash test.sh 192.168.3.59
192.168.3.59 is online!
[root@centos7 test2]# bash test.sh 192.168.3.70
192.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: " SCORE
if [ $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!"
else
echo "out of the range! the range is 0-100"
fi
[root@centos7 test2]# bash test.sh
please input your score: 97
excellent!
[root@centos7 test2]# bash test.sh
please input your score: 86
pass!
[root@centos7 test2]# bash test.sh
please input your score: 34
failure!
[root@centos7 test2]# bash test.sh
please input your score: 342
out of the range! the range is 0-100
[root@centos7 test2]# bash test.sh
please input your score: -342
out 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: " PASSWD
for i in `cat a.txt`
do
id $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i had existed!"
else
useradd $i &> /dev/null
echo $PASSWD | passwd --stdin $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i had created successfully!"
else
echo "$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

相当于:

[root@centos7 test2]# cat a.txt
abcde02
abcde03
abcde04
abcde05
abcde06
abcde07
abcde08
[root@centos7 test2]# cat test.sh
#!/bin/bash
read -p "please input the passwd: " PASSWD
cat a.txt | while read i
do
id $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i had existed!"
else
useradd $i &> /dev/null
echo $PASSWD | passwd --stdin $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i had created successifully!"
else
echo "$i created failed!"
fi
fi
done
[root@centos7 test2]# bash test.sh
please input the passwd: abc123456
abcde02 had created successifully!
abcde03 had created successifully!
abcde04 had created successifully!
abcde05 had created successifully!
abcde06 had created successifully!
abcde07 had created successifully!
abcde08 had created successifully!

 

 

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.com
11111111
[root@centos7 test2]# cat test.sh
#!/bin/bash
for i in `cat a.txt`
do
ping -c 3 -w 6 $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i is online!"
else
echo "$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!

相当于:

[root@centos7 test2]# cat a.txt
www.baidu.com
sssssssssssyyyyy
ewrrewrererewer
www.jd.com
www.taobao.com
www.tencent.com
3333333333333
[root@centos7 test2]# cat test.sh
#!/bin/bash
cat a.txt | while read i
do
ping -c 3 -i 0.2 -w 8 $i &> /dev/null
if [ $? -eq 0 ]
then
echo "$i is online!"
else
echo "$i is offline!"
fi
done
[root@centos7 test2]# bash test.sh
www.baidu.com is online!
sssssssssssyyyyy is offline!
ewrrewrererewer is offline!
www.jd.com is online!
www.taobao.com is online!
www.tencent.com is online!
3333333333333 is offline!

 

7、while语句

[root@centos7 test2]# cat test.sh
#!/bin/bash
PRICE=$(expr $RANDOM % 1000)
echo "the price range is 0-999, please guess!"
times=0
while true
do
read -p "please input the price: " tmp
let times++
if [ $tmp -eq $PRICE ]
then
echo "you are right, the price is $PRICE!"
echo "you had guess $times times!"
exit 0
elif [ $tmp -gt $PRICE ]
then
echo "too hight!, try again!"
else
echo "too low, try again!"
fi
done
[root@centos7 test2]# bash test.sh
the price range is 0-999, please guess!
please input the price: 500
too hight!, try again!
please input the price: 250
too low, try again!
please input the price: 375
too hight!, try again!
please input the price: 300
too hight!, try again!
please input the price: 275
too low, try again!
please input the price: 285
too low, try again!
please input the price: 290
too low, try again!
please input the price: 295
too low, try again!
please input the price: 298
too hight!, try again!
please input the price: 296
you are right, the price is 296!
you had guess 10 times!

 

8、case语句

[root@centos7 test2]# ls
test.sh
[root@centos7 test2]# cat test.sh
#!/bin/bash
read -p "please input an character: " key
case $key in
[a-z]|[A-Z])
echo "letter!"
;;
[0-9])
echo "number!"
;;
*)
echo "other!"
esac
[root@centos7 test2]# bash test.sh
please input an character: 8
number!
[root@centos7 test2]# bash test.sh
please input an character: d
letter!
[root@centos7 test2]# bash test.sh
please input an character: !
other!

 

posted @ 2021-04-21 23:16  小鲨鱼2018  阅读(338)  评论(0编辑  收藏  举报