shell腳本基礎(二)

1、shell腳本中的邏輯判斷

一、判斷語句if

a)不帶else

格式:linux

if 判斷語句;then 
   command
fi

實例:shell

[root@zlinux-01 shell]# vim if01.sh          //判斷數值大小第一種方法用[],注意先後空格

#! /bin/bash
a=5
if [ $a -gt 3 ];then
    echo "這個數字大於3"
fi
#-gt:大於,-lt:小於,-ge:大於或等於,-le:小於或等於,-eq:等於,-ne:不等於

[root@zlinux-01 shell]# sh if01.sh 
這個數字大於3
[root@zlinux-01 shell]# vim if02.sh                //判斷數值大小的第二種方式:(())

#! /bin/bash
a=5
if ((a>3));then
   echo "這個數字大於3"
fi
#((a>3)),雙括號是shell中的特有格式,只用一個括號或者不用都會報錯

[root@zlinux-01 shell]# sh if02.sh 
這個數字大於3

b)帶有else

格式:vim

if 判斷語句;then 
   command
else
   command
fi

實例:bash

[root@zlinux-01 shell]# vim ifelse.sh 

#! /bin/bash
read -p "請輸入分數:" a
if [ $a -lt 60 ];then
   echo "你沒有經過測試!"
else
   echo "恭喜你,順利經過測試!"
fi
#read命令用於和用戶交互,它把用戶輸入的字符串做爲變量值 

[root@zlinux-01 shell]# sh ifelse.sh 
請輸入分數:60
恭喜你,順利經過測試!
[root@zlinux-01 shell]# sh ifelse.sh 
請輸入分數:59
你沒有經過測試!

c)帶有elif

格式:ide

if 判斷語句1;then
   command
elif 判斷語句2;then
   command
else
   command
fi

實例:測試

[root@zlinux-01 shell]# vim elif.sh 

#! /bin/bash
read -p "請輸入分數:" a
if (($a<60));then
   echo "未經過測試。"
elif ((a>=60))&&(($a<85));then
   echo "經過測試,成績良好。"
else
   echo "經過測試,成績優秀!"
fi
# &&表示而且,固然也能夠用||表示或者

[root@zlinux-01 shell]# sh elif.sh 
請輸入分數:60
經過測試,成績良好。
[root@zlinux-01 shell]# sh elif.sh 
請輸入分數:34
未經過測試。
[root@zlinux-01 shell]# sh elif.sh 
請輸入分數:99
經過測試,成績優秀!

二、和文檔相關的判斷

shell中if嗨常常用於判斷文檔屬性。經常使用選項有:code

-e:判斷文件或目錄是否存在;
-d:判斷是否是目錄以及是否存在;
-f:判斷是不是文件以及是否存在;
-r:判斷是否有讀權限;
-w;判斷是否有寫權限;
-x:判斷是否有執行權限。文檔

格式:字符串

if [ -e filename ];then
   command
fi

實例:input

[root@zlinux-01 shell]# if [ -d /home/ ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -f /home/ ]; then echo ok; fi                //home是目錄不是文件,因此沒有輸出ok
[root@zlinux-01 shell]# touch /root/test.txt
[root@zlinux-01 shell]# if [ -f /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -r /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -w /root/test.txt ]; then echo ok; fi
ok
[root@zlinux-01 shell]# if [ -x /root/test.txt ]; then echo ok; fi
[root@zlinux-01 shell]# if [ -e /root/test1.txt ]; then echo ok; fi
[root@zlinux-01 shell]# if [ -d /root/test.txt ]; then echo ok; fi

三、case邏輯判斷

格式:

case 變量 in
value1)
              command
                            ;;
value2)
              command
                            ;;
value3)
              command
                            ;;
*)
              command
                            ;;
esac

不限制value的個數,*表示其餘值
實例:

[root@zlinux-01 shell]# vim case.sh

#! /bin/bash
read -p "請輸入一個數字:" n
a=$[$n%2]
case $a in
  1)
      echo "這數字是奇數"
      ;;
  0)
      echo "這數字是偶數"
      ;;
  *)
      echo "這個不是數字"
      ;;
esac

[root@zlinux-01 shell]# sh case.sh
請輸入一個數字:3
這數字是奇數
[root@zlinux-01 shell]# sh case.sh
請輸入一個數字:2
這數字是偶數

四、練習(輸入考試成績,判斷得分等級)

[root@zlinux-01 shell]# vim test01.sh

#!/bin/bash
read -p "Please input a number: " n
if [ -z "$n" ]
then
    echo "Please input a number."
    exit 1
#「exit 1」表示執行該部分命令後的返回值
#即,命令執行完後使用echo $?的值
fi

n1=`echo $n|sed 's/[0-9]//g'`
#判斷用戶輸入的字符是否爲純數字
#若是是數字,則將其替換爲空,賦值給$n1
if [ -n "$n1" ]
then
 echo "Please input a number."
 exit 1
#判斷$n1不爲空時(即$n不是純數字)再次提示用戶輸入數字並退出
fi

#若是用戶輸入的是純數字則執行如下命令:
if [ $n -lt 60 ] && [ $n -ge 0 ]
then
    tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]
then
    tag=2
elif [ $n -ge 80 ]  && [ $n -lt 90 ]
then
    tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]
then
    tag=4
else
    tag=0
fi
#tag的做用是爲判斷條件設定標籤,方便後面引用
case $tag in
    1)
        echo "not ok"
        ;;
    2)
        echo "ok"
        ;;
    3)
        echo "ook"
        ;;
    4)
        echo "oook"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac

[root@zlinux-01 shell]# sh test01.sh 
Please input a number: aa
Please input a number.
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 78
ok
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 90
oook
[root@zlinux-01 shell]# sh test01.sh 
Please input a number: 11
not ok
相關文章
相關標籤/搜索