码迷,mamicode.com
首页 > 系统相关 > 详细

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

时间:2021-04-22 16:20:40      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:ase   shell   err   offline   led   cat   流程   st3   www   

 

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

 

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!

 

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

标签:ase   shell   err   offline   led   cat   流程   st3   www   

原文地址:https://www.cnblogs.com/liujiaxin2018/p/14687470.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!