【3.2】shell script条件判断式

4.条件判断式

4.1 if then

if [ 条件判断式 ]; then
		 当条件判断式成立时,可以进行的指令工作内容;
fi   <==将 if 反过来写,就成为 fi 啦!结束 if 之意!
&& 代表 AND ;
|| 代表 or ;

所以,在使用中括号的判断式中, && 及 || 就与指令下达的状态不同了。举例来说, sh06.sh 里面的判断式可以这样修改:

[ "$yn" == "Y" -o "$yn" == "y" ]
上式可替换为
[ "$yn" == "Y" ] || [ "$yn" == "y" ]


[sam] vi sh06-2.sh

#!/bin/bash
#Program:
#   This program shows the user's choice
#History:
#2014/07/22  Sam 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

#一个条件判断,分成功进行与失败进行 (else)

if [ 条件判断式 ]; then

当条件判断式成立时,可以进行的指令工作内容;

else

当条件判断式不成立时,可以进行的指令工作内容;

fi

如果考虑更复杂的情况,则可以使用这个语法:

# 多个条件判断 (if ... elif ... elif ... else) 分多种不同情况执行

if [ 条件判断式一 ]; then

当条件判断式一成立时,可以进行的指令工作内容;

elif [ 条件判断式二 ]; then

当条件判断式二成立时,可以进行的指令工作内容;

else

当条件判断式一与二均不成立时,可以进行的指令工作内容;

fi

[sam] vi sh06-3.sh

#!/bin/bash
#Program:
#   This program shows the user's choice
#History:
#2014/07/22  Sam 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

[sam] vi sh09.sh

#!/bin/bash
# Program:
#   Check $1 is equal to "hello"
#History:
# 2014/07/22 Sam 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 [ "$1" == "" ];then
   echo "You must input parameters,ex>{$0 someword}"
else
   echo "The only parameter is 'hello' , ex>{$0 hello}"
fi

[sam] vi sh10.sh

#!/bin/bash
# Program:
#  You input your demobilization date ,I calculate how many days
#  before you demobilize.
# History:
#  2014/07/22  Sam  First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
# 1. 告知用户这支程序的用途,并且告知应该如何输入日期格式?
echo "This program will try to calculate :"
echo "How many days before your demobilization date..."
read -p "Please input your demobilization date (YYYYMMDD ex>20090401): " date2
# 2. 测试一下,这个输入的内容是否正确?利用正规表示法啰~
date_d=$(echo $date2 |grep '[0-9]\{8\}')   # 看看是否有八个数字
if [ "$date_d" == "" ]; then
				echo "You input the wrong date format...."
				exit 1
fi

3. 开始计算日期啰~

declare -i date_dem=`date --date="$date2" +%s`    # 退伍日期秒数
declare -i date_now=`date +%s`                    # 现在日期秒数
declare -i date_total_s=$(($date_dem-$date_now))  # 剩余秒数统计
declare -i date_d=$(($date_total_s/60/60/24))     # 转为日数
if [ "$date_total_s" -lt "0" ]; then              # 判断是否已退伍
				echo "You had been demobilization before: " $((-1*$date_d)) " ago"
else
				declare -i date_h=$(($(($date_total_s-$date_d*60*60*24))/60/60))
				echo "You will demobilize after $date_d days and $date_h hours."
fi

利用 case ….. esac 判断

case $变量名称 in <==关键词为 case ,还有变数前有钱字号
"第一个变量内容") <==每个变量内容建议用双引号括起来,关键词则为小括号 )

程序段
;; <==每个类别结尾使用两个连续的分号来处理!
"第二个变量内容")

程序段
;;
*) <==最后一个变量内容都会用 * 来代表所有其他值
不包含第一个变量内容与第二个变量内容的其他程序执行段
exit 1
;;
esac <==最终的 case 结尾!『反过来写』思考一下!

每一个变量内容的程序段最后都需要两个分号 (;;) 来代表该程序段落的结束,这挺重要的喔! 至于为何需要有 * 这个变量内容在最后呢?这是因为,如果用户不是输入变量内容一或二时, 我们可以告知用户相关的信息啊.

[sam] vi sh09-2.sh

#!/bin/bash
# Program:
#   Show "Hello" from $1 ... by using case ....esac
#History:
#   2014/07/22  Sam 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}"
	;;
  *)   # 其实就相当于通配符,0~无穷多个任意字符之意!
	echo "Usage $0 {hello}"
	;;
esac

[sam] vi sh12.sh

#!/bin/bash
#Program:
# This scripty only sccepts the flowing parameter; one ,two or three.
#History:
#  2014/07/22 Sam First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
echo "This program will print your selection !"
# read -p "Input your choice: " choice # 暂时取消,可以替换!
# case $choice in                      # 暂时取消,可以替换!
case $1 in                             # 现在使用,可以用上面两行替换!
  "one")
				echo "Your choice is ONE"
				;;
  "two")
				echo "Your choice is TWO"
				;;
  "three")
				echo "Your choice is THREE"
				;;
  *)
				echo "Usage $0 {one|two|three}"
				;;
esac

funciton的使用

因为 shell script 的执行方式是由上而下,由左而右, 因此在 shell script 当中的 function 的设定一定要在程序的最前面,

[root@www scripts]# vi sh12-2.sh

#!/bin/bash
# Program:
#             Use function to repeat information.
# History:
# 2005/08/29   VBird        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 可以不断行继续在同一行显示
}
 
echo "This program will print your selection !"
case $1 in
  "one")
				printit; echo $1 | tr 'a-z' 'A-Z'  # 将参数做大小写转换!
				;;
  "two")
printit; echo $1 | tr 'a-z' 'A-Z'
				;;
  "three")
				printit; echo $1 | tr 'a-z' 'A-Z'
				;;
  *)
				echo "Usage $0 {one|two|three}"
				;;
esac

另外, function 也是拥有内建变量的~他的内建变量与 shell script 很类似, 函数名称代表示 $0 ,而后续接的变量也是以 $1, $2… 来取代的~

[root@www scripts]# vi sh12-3.sh

#!/bin/bash
# Program:
#             Use function to repeat information.
# History:
# 2005/08/29   VBird        First release
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH
 
function printit(){
				echo "Your choice is $1"   # 这个 $1 必须要参考底下指令的下达
}
 
echo "This program will print your selection !"
case $1 in
  "one")
				printit 1  # 请注意, printit 指令后面还有接参数!
				;;
  "two")
				printit 2
				;;
  "three")
				printit 3
				;;
  *)
				echo "Usage $0 {one|two|three}"
				;;
esac

参考资料:

http://linux.vbird.org/linux_basic/0340bashshell-scripts.php

这里是一个广告位,,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn