Shell script的语法四:条件判断语句

Shell script的条件判断语句有以下几种:

一、使用if…then

1、简单条件判断语句

1)格式:

if [ 条件判断式 ]; then
	当条件判断式成立时,可以进行命令工作的內容;
fi   <==将 if 反过来写,结束 if 
其中条件判断式可以多个中括号隔开,而括号与括号间,则以&&或||来隔开,其用法为:&& 代表 AND ;|| 代表 or ;

2)示例

#!/bin/bash
#Program:
#  This program shows the usr's choice
#History:
#2017/01/11 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input(Y/N):" yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
     echo "OK,continue."
     exit 0
fi
if [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
     echo "Oh,interrupt!"
     exit 0
fi
echo "I don't know what your choice is" && exit 0
运行结果:

root@Test:~/bin$ chmod a+x ans_yn.sh;./ans_yn.sh
Please input(Y/N):y
OK,continue.

二、多重条件判断语句

1、一个条件判断,分成功与失败2种情况时,可使用以下语法:

if [ 条件判断语句 ]; then
	当条件判断语句成立时,进行的命令内容;
else
	当条件判断语句不成立时,进行的命令内容;
fi
2、当使用多个条件判断时,分多种不同情况执行,使用以下语法:

# if ... elif ... elif ... else
if [ 条件判断语句一 ]; then
	当条件判断语句一成立时,进行的命令内容;
elif [ 条件判断语句二 ]; then
	当条件判断语句二成立时,进行的命令内容;
else
	当条件判断语句一与二均不成立时,进行的命令内容;
fi
语法中elif 也是个判断语句,因此出现 elif 后面都要接 then ,但是 else 已经是最后一个没有成立的结果, 所以 else 后面不需要 then 。

3、示例

1)使用简单判断语句的程序可以修改如下:

#!/bin/bash
#Program:
#  This program shows the usr's choice
#History:
#2017/01/11 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input(Y/N):" yn
if [ "${yn}" == "Y" ] || [ "${yn}" == "y" ]; then
     echo "OK,continue."
elif [ "${yn}" == "N" ] || [ "${yn}" == "n" ]; then
     echo "Oh,interrupt!"
else
     echo "I don't know what your choice is"
fi

2)使用复杂判断语句的示例

#!/bin/bash
#Program:
#  Check $1 is equal to "hello"
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
if [ "${1}" == "hello" ];then
     echo "Hello, how are you ?"
elif [ -z "${1}" ];then
     echo "You must input parameters,ex>{${0} someword}"
else
     echo "Please input hello,ex>{${0} hello}"
fi
运行结果:

root@Test:~/bin$ chmod a+x helloif.sh;./helloif.sh hello
Hello, how are you ?
root@Test:~/bin$ chmod a+x helloif.sh;./helloif.sh
You must input parameters,ex>{./helloif.sh someword}

二、使用case ... in .... esac

1、语法如下:

 case  $变量名称 in   #关键字为 case ,变量名称前需加上$
  "第一个变量值")      #每个变量值建议用双引号括起來,关键字则为小括号
	实现程序代码
	;;                       #每个类别结尾使用两个连续的分号
  "第二個变量值")
	实现程序代码
	;;
  *)                             #最后一个变量值使用 * 來代表所有其他值
	实现不包含第一个变量值和第二个变量值的其他程序代码
	exit 1
	;;
esac                          #最后以 esac 结尾

另外,在运行时实例化$变量,可采用2种方式:

1)直接命令下发:运行命令时直接給 $变量 赋值,这也在 /etc/init.d 目录下大多数程序的设计方式。

2)互动式:在程序中通过 read 指令来让用户输入变量值的內容。

2、示例

1)示例1,使用直接命令下发方式对上面复杂判断语句的程序进行修改:

#!/bin/bash
#Program:
#  Check $1 is equal to "hello"
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
case ${1} in
 "hello")
     echo "Hello, how are you ?"
     ;;
 "")
     echo "You must input parameters,ex>{${0} someword}"
     ;;
 *)
     echo "Please input hello,ex>{${0} hello}"
     ;;
esac
运行结果:

root@Test:~/bin$ chmod a+x hellocase.sh;./hellocase.sh hello
Hello, how are you ?
root@Test:~/bin$ chmod a+x hellocase.sh;./hellocase.sh hel
Please input hello,ex>{./hellocase.sh hello}
root@Test:~/bin$ chmod a+x hellocase.sh;./hellocase.sh 
You must input parameters,ex>{./hellocase.sh someword}
2)示例2,使用互动式对上面复杂判断语句的程序进行修改:
#!/bin/bash
#Program:
#  Check $1 is equal to "hello"
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
read -p "Please input your choice:" choice
case ${choice} in
 "one")
     echo "Your choice is one."
     ;;
 "two")
     echo "Your choice is two."
     ;;
 "three")
     echo "Your choice is three."
     ;;
 *)
     echo "Usage ${0} {one|two|theree}"
     ;;
esac
运行结果:

root@Test:~/bin$ sh number.sh 
Please input your choice:one
Your choice is one.
root@Test:~/bin$ sh number.sh 
Please input your choice:d
Usage number.sh {one|two|theree}


三、使用function

1、function为自定义函数,其语法:

function fname() {
	程序代码
}

其中:fname 是自定义函数的名称

注意:因为 shell script 的执行方式:由上而下,由左而右, 因此在 shell script 当中的 function 的设定一定要在程序的最前面, 这样才能被程序引用,这一点与其他程序语言有很大的区别。

例如,改写上面的程序:

#!/bin/bash
#Program:
#  Check $1 is equal to "hello"
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

function printit(){
     echo -n "Your choice is " #加上-n可以不断行继续在同一行显示
}

read -p "Please input your choice:" choice
case ${choice} in
 "one")
     printit; echo ${choice} | tr 'a-z' 'A-Z' #将参数进行大小写转换
     ;;
 "two")
     printit; echo ${choice}
     ;;
 "three")
     echo "Your choice is three."
     ;;
 *)
     echo "Usage ${0} {one|two|theree}"
     ;;
esac
运行结果:

root@Test:~/bin$ chmod a+x numberf.sh;./numberf.sh 
Please input your choice:one
Your choice is ONE
root@Test:~/bin$ chmod a+x numberf.sh;./numberf.sh 
Please input your choice:two
Your choice is two
root@Test:~/bin$ chmod a+x numberf.sh;./numberf.sh 
Please input your choice:three
Your choice is three.


2、function 的自定义函数可以带参数,它的参数与shell script的命令参数类似,但并不相同。

     其中,${0} 表示函数名称;函数名称后面接的变量也是以${1} ,${2} ...来取代。

例如:

#!/bin/bash
#Program:
#  Check $1 is equal to "hello"
#History:
#2017/01/10 shu First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

function printit(){
     echo -n "Your choice is ${1}" #加上-n可以不断行继续在同一行显示
}

echo "This program will print your selection."
case ${1} in
 "one")
     printit 1;     
     ;;
 "two")
     printit 2;
     ;;
 "three")
     printit 3;
     ;;
 *)
     echo "Usage ${0} {one|two|theree}"
     ;;
esac
运行结果:

root@Test:~/bin$ chmod a+x numberf1.sh;./numberf1.sh one
This program will print your selection.
Your choice is 1



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值