Linux学习之Shell Scripts(三)

一、条件判断式:if.....then

(1)单层、简单条件判断式

if[ 条件判断式 ];then
        当条件判断式成立时,可以进行的指令工作内容;
fi  <== 代表结束

&& 代表AND

|| 代表or

(2)多重复杂判断式

#一个条件判断,分成功进行与失败进行(else)
if [ 条件判断式 ];then
        当条件判断为True时,可以执行指令内容
else
        当条件判断为False时,可以执行指令内容
fi


#多个条件判断分多种不同情况执行
if [ 条件判断式一 ];then
        当条件判断式一为true时,可以执行指令内容
elif [ 条件判断式二 ];then
        当条件判断式二为true时,可以执行指令内容
else
        当条件判断式一和二都为false时,可以执行指令内容
fi

(3)例题

eg1:

       1、判断$1是否为dalianmao,如果是的话,就显示“Hi,dalianmao!”

       2、如果$1为空,就显示“You must input parameters!”

      3、如果$1不是dalianmao,就显示“You input error parameter!"

[dalianmao@localhost ~]$ vi test.sh
[dalianmao@localhost ~]$ sh test.sh 
You must input parameters!
[dalianmao@localhost ~]$ sh test.sh sb
You input error parameter!
[dalianmao@localhost ~]$ sh test.sh dalianmao
Hi,dalianmao!
[dalianmao@localhost ~]$ nl test.sh 
     1	#!/bin/bash
       
     2	#Program:
       
     3	#   Check parameter $1 is equal to "dalianmao"
       
     4	#History:
     5	#    2018/11/13 dalianmao v1.0
       
     6	PATH=/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin:~/bin
     7	export PATH
       
     8	if [ "$1" == "dalianmao" ];then
     9			echo "Hi,dalianmao!"
    10	elif [ -z "$1" ];then
    11			echo "You must input parameters!"
    12	else
    13			echo "You input error parameter!"
       
    14	fi
       
[dalianmao@localhost ~]$ 

eg2:撰写一个脚本,计算你还有几天可以过生日

[dalianmao@localhost ~]$ sh birthday_caculate.sh 
Please input your birthday!(eg.1104):1115
Happy birthday to you!
[dalianmao@localhost ~]$ sh birthday_caculate.sh 
Please input your birthday!(eg.1104):1116
your birthday will be 1 later!
[dalianmao@localhost ~]$ sh birthday_caculate.sh 
Please input your birthday!(eg.1104):1113
your birthday will be 363 later!
[dalianmao@localhost ~]$ nl birthday_caculate.sh 
     1	#!/bin/bash
       
     2	read -p "Please input your birthday!(eg.1104):" birth
       
     3	today=$(date +%m%d)
       
     4	if [ $birth == $today ];then
     5		echo "Happy birthday to you!"
     6	elif [ $birth -gt $today ];then
     7		year=`date +%Y`
     8	        sub_days=$(($((`date --date="$year$birth" +%s` - `date +%s`))/3600/24 + 1))
     9		echo "your birthday will be $sub_days later!"
       
    10	else  
    11		year=$((`date +%Y`+1))
    12		sub_days=$(($((`date --date="$year$birth" +%s` - `date +%s`))/3600/24 +1))
    13		echo "your birthday will be $sub_days later!"
       
    14	fi

   

二、条件判断式:case .....esac

case $变量名 in 

"第一个变量内容")

    程序段

    ;;
"第二个变量内容")

    程序段
    ;;

"第三个变量内容")

    程序段
    ;;

esac    =====>case反写,表示结束,跟if 和fi一样,哈哈哈

eg.输入one、two、three 三个,打印,如果不是提示只能选这三个

[dalianmao@localhost ~]$ sh choice.sh one
Your choice is one!
[dalianmao@localhost ~]$ sh choice.sh two
Your choice is two!
[dalianmao@localhost ~]$ sh choice.sh three
Your choice is three!
[dalianmao@localhost ~]$ sh choice.sh haha
Usage choice.sh (one|two|three
[dalianmao@localhost ~]$ nl choice.sh 
     1	#!/bin/bash
       
       
     2	case $1 in
       
       
     3	"one")
     4		echo "Your choice is one!"
     5		;;
       
     6	"two")
     7		echo "Your choice is two!"
     8		;;
     9	"three")
    10		echo "Your choice is three!"
    11		;;
       
    12	*)
    13		echo "Usage $0 (one|two|three"
    14		;;
    15	esac
       

 

你可能感兴趣的:(Linux学习之Shell Scripts(三))