目录
多分支
语法
if <条件表达式1>; then
指令1
elif <条件表达式2>; then
指令2
...
else
指令n
fi
当整个 if elif 结构是不满足第一个条件进,则执行进入第二个条件表达式,如果依然不满足则进入第三个,依次类推,当都不满足时则进入 else 语句中,如果某个条件满足则执行对应的指令
示例1:
就收两个整数并比较其大小
分析:
1、接收数据,我们需要使用 read 指令
read -p “提示信息” a b
2、对两个数进行比较
代码实现
[root@openEuler ~]# cat compare.sh
#!/bin/bash
read -p "please input two number:" a b
if [ $a -eq $b ]; then
echo "$a equals $b"
elif [ $a -gt $b ]; then
echo "$a greate then $b"
elif [ $a -lt $b ]; then
echo "$a less then $b"
else
echo "error"
fi
[root@openEuler ~]# bash compare.sh
please input two number:2 3
2 less then 3
[root@openEuler ~]# bash compare.sh
please input two number:5 2
5 greate then 2
[root@openEuler ~]# bash compare.sh
please input two number:5 5
5 equals 5
方式二:通过执行脚本时传参,而不是通过 read 读取
代码示例
[root@openEuler ~]# cat compare2.sh
#!/bin/bash
if [ $# -ne 2 ]; then
echo "parameter must two"
exit 1
fi
if [ $1 -eq $2 ]; then
echo "$1 equals $2"
elif [ "$1" -gt "$2" ]; then
echo "$1 greate than $2"
else
echo "$1 less than $2"
fi
[root@openEuler ~]# bash compare2.sh 4
parameter must two
[root@openEuler ~]# bash compare2.sh
parameter must two
[root@openEuler ~]# bash compare2.sh 4 5
4 less than 5
示例2:
根据输入的成绩,判断成绩的优良中差
85~100为优,A
70~84为良,B
60~69为合格,C
60以下为不合格,D
分析:
1、接收输入的成绩,并且存入到变量 score 中
read -p "input score" score
2、判断输入的分数是否为空
if [ -z $score ] ; then echo "不能为空" fi
3、判断输入的成绩是否 小于0 或者 大于100
if [ $score -lt 0 -o $score -gt 100 ] ; then echo "不对" fi
4、判断输入的成绩是否在 85~100 之间
if [ $score -ge 85 -a $score -le 100 ] ; then echo "A" fi
5、判断输入的成绩是否在 70~84 之间
if [ $score -ge 70 -a $score -le 84 ] ;then echo "B" fi
6、判断输入的成绩是否在 60~69 之间
if [ $score -ge 60 -a $score -le 69 ] ; then echo "C" fi
7、判断输入的成绩是否在 60以下
if [ $score -lt 60 ] ; then echo "D" fi
代码实现:
[root@openEuler ~]# cat score.sh
#!/bin/bash
read -p "please your score:" score
if [ -z $score ] ; then
echo "score not empty"
exit 1
elif [[ $score -lt 0 || $score -gt 100 ]] ; then
echo "score must between 0 and 100"
exit 2
elif [ $score -ge 85 -a $score -le 100 ] ; then
echo "A"
elif [ $score -ge 70 -a $score -le 84 ] ; then
echo "B"
elif [[ $score -ge 60 && $score -le 69 ]] ; then
echo "C"
else
echo "D"
fi
[root@openEuler ~]# bash score.sh
please your score:-1
score must between 0 and 100
[root@openEuler ~]# bash score.sh
please your score:
score not empty
[root@openEuler ~]#
[root@openEuler ~]# bash score.sh
please your score:59
D
[root@openEuler ~]# bash score.sh
please your score:67
C
[root@openEuler ~]# bash score.sh
please your score:98
A
注意:
如果在判断语句中使用 && 或者 || 时,需要使用双中括号[[ ]]
退出程序
在 shell 中,我们可以使用 exit 命令来退出脚本,并且可以返回指定的状态码,以便于后续的判断
语法格式
exit status
注意:
status 是一个数字,当值为 0 时表示命令正常结束,当为非 0 时,表示命令执行时出现了错误
使用示例:
在不同的情况下,程序返回不同一状态码
代码示例:
[root@openEuler ~]# cat exit.sh
#!/bin/bash
echo hello world
echo $?
haha
echo $?
exit 100
[root@openEuler ~]# bash exit.sh
hello world
0
exit.sh: line 6: haha: command not found
127
[root@openEuler ~]# echo $?
100
[root@openEuler ~]#
exit还可以使用在 if 语句中,使得程序在不同条件下退出
代码示例:
[root@openEuler ~]# cat file_create.sh
#!/bin/bash
if [ -e "$1" ]; then
echo "file $1 exists"
exit 1
else
echo "file $1 is not exists"
touch $1
exit 0
fi
[root@openEuler ~]# bash file_create.sh aa
file aa is not exists
[root@openEuler ~]# bash file_create.sh aa
file aa exists
[root@openEuler ~]# ll
total 28
-rw-r--r--. 1 root root 0 Mar 31 16:34 aa
case语句
语法
case 变量名 in
值1)
指令1
;;
值2)
指令2
;;
值n)
指令n
;;
*)
默认指令
esac
case语句,会将变量的值与每个值进行比较,如果与某个值相等,则执行该指令,当遇到 ";;" 符号时,表示退出 case语句,执行后续代码。如果没有任何一个值与之匹配,则执行 ”*“ 后面的语句
使用示例1:
由用户从键盘输入一个字符,判断该字符是字母还是数字,以及其他字符,并输入相应的提示信息
分析:
1、接收用户输入
read -p "" key
2、判断是否为空
[ -z $key ]
3、判断是否为字母
[a-z] | [A-Z]
4、判断是否为数字
[0-9]
5、如果第3步和第4步都没有匹配上,则表示是其他字符
代码示例:
[root@openEuler ~]# cat test.sh
#!/bin/bash
read -p "please input character" key
if [ -z $key ] ; then
echo "input must not empty"
exit 2
fi
if [ $(expr length $key) -ne 1 ] ; then
echo "input length must 1"
exit 3
fi
case $key in
[a-z]|[A-Z])
echo "input is letter"
;;
[0-9])
echo "input is number"
;;
*)
echo "input is other character"
;;
esac
[root@openEuler ~]# bash test.sh
please input character: c
input is letter
[root@openEuler ~]# bash test.sh
please input character: 4
input is number
[root@openEuler ~]# bash test.sh
please input character: ;
input is other character
使用示例2:
根据输入的成绩,判断成绩的优良中差
85~100为优,A
70~84为良,B
60~69为合格,C
60以下为不合格,D
代码示例:
[root@openEuler ~]# cat score2.sh
#!/bin/bash
read -p "input your score: " score
case ${score} in
8[5-9]|9[0-9]|100)
echo "A"
;;
7[0-9]|8[0-4])
echo "B"
;;
6[0-9])
echo "C"
;;
*)
echo "D"
esac
[root@openEuler ~]# bash score2.sh
input your score: 59
D
[root@openEuler ~]# bash score2.sh
input your score: 67
C
[root@openEuler ~]# bash score2.sh
input your score: 75
B
[root@openEuler ~]# bash score2.sh
input your score: 89
A
练习:
开发一个rsync起停脚本
代码示例:
[root@openEuler ~]# cat my_rsync.sh
#!/bin/bash
if [ "$#" -ne 1 ] ; then
echo "Usage: $0 {start|stop|restart}"
exit 1
fi
case "$1" in
"start")
/usr/bin/rsync --daemon
sleep 1
if [ `ss -lntup|grep rsync|wc -l` -ge 1 ] ; then
echo "rsync is start"
exit 0
fi
;;
"stop")
killall rsync &>/dev/null
sleep 1
if [ `ss -lntup|grep rsync|wc -l` -eq 0 ] ; then
echo "rsync is stop"
exit 0
fi
;;
"restart")
if [ `ss -lntup|grep rsync|wc -l` -ge 1 ] ; then
killall rsync &>/dev/null
sleep 1
fi
/usr/bin/rsync --daemon
sleep 1
echo "rsync is restarted"
exit 0
;;
*)
echo "Usage: $0 {start|stop|restart|"
esac
[root@openEuler ~]# bash my_rsync.sh
Usage: my_rsync.sh {start|stop|restart}
[root@openEuler ~]# bash my_rsync.sh start
rsync is start
[root@openEuler ~]# bash my_rsync.sh stop
rsync is stop
[root@openEuler ~]# bash my_rsync.sh restart
rsync is restarted