7月12日任務 shell腳本中的邏輯判斷 、文件目錄屬性判斷 、 if特殊用法 、case判斷

20.5 Shell腳本中的邏輯判斷

格式1:if 條件 ; then 語句; filinux

格式2:if 條件; then 語句; else 語句; fishell

格式3:if …; then … ;elif …; then …; else …; fibash

邏輯判斷表達式:if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等 -gt (>); -lt(<); -ge(>=); -le(<=);-eq(==); -ne(!=) 注意處處都是空格spa

能夠使用 && || 結合多個條件code

if [ $a -gt 5 ] && [ $a -lt 10 ]; then文檔

 if [ $b -gt 5 ] || [ $b -lt 3 ]; theninput

在[ ]中括號中:it

-lt:=little than 小於  <變量

-le:=little && equal 小於等於  <=sed

-eq:=equal 等於  ==

-ne:=no equal 不等於  !=

-gt:=greater than 大於  >

-ge:=greater && equal 大於等於  >=

能夠用用符號,但要在(( ))小括號中:相似: if (($a>1)); then echo ok;fi 

ok
<,<=,==,!=,>,>=

注意: 使用雙括號括起來

格式1:if 條件 ; then 語句; fi 

#!/bin/bash
a=5
 if [ $a -gt 3 ]  #注意[]內的空格
 then
      echo ok
 fi

格式2:if 條件; then 語句; else 語句; fi

#!/bin/bash
a=3
if [ $a -gt 3 ]
then
echo ok
else
echo no ok 
fi

格式3:if …; then … ;elif …; then …; else …; fi

#!/bin/bash
a=1
if [ $a -lt 3 ]
then
echo "<3"
elif [ $a -lt 6 ]
then
echo "<6 && >3"
else
echo no ok
fi

關係

各個條件之間的關係能夠使用邏輯鏈接符:

條件A&&條件B:而且
條件A||條件B:或者

• if [ $a -gt 5 ] && [ $a -lt 10 ]; then

• if [ $b -gt 5 ] || [ $b -lt 3 ]; then

 

20.6 文件目錄屬性判斷

shell腳本中if常常用於判斷文檔的屬性,好比判斷是普通文件仍是目錄文件,判斷文件是否有讀、寫、執行權限等。if經常使用的選項有如下幾個:

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

格式 

 [ -f file ]判斷是不是普通文件,且存在

#!/bin/nash
f="/tmp/lxy"
if [ -f $f ]
then
echo $f exist
else
touch  $f
fi

 [ -d file ] 判斷是不是目錄,且存在

#!/bin/nash
f="/tmp/lxy"
if [ -d $f ]
then
echo $f exist
else
touch  $f
fi

[ -e file ] 判斷文件或目錄是否存在 

#!/bin/nash
f="/tmp/lxy"
if [ -e $f ]
then
echo $f exist
else
touch  $f
fi

[ -r file ] 判斷文件是否可讀

#!/bin/nash
f="/tmp/lxy"
if [ -r $f ]
then
echo $f read
fi

[ -w file ] 判斷文件是否可寫

#!/bin/nash
f="/tmp/lxy"
if [ -w $f ]
then
echo $f read
fi

[ -x file ] 判斷文件是否可執行 

#!/bin/nash
f="/tmp/lxy"
if [ -x $f ]
then
echo $f read
fi

 另一種用法,判斷這個文件或者目錄存不存在,若是存在就刪除,「&&」表示當前面的命令執行成功的時候纔會執行後面的命令 。「||」表示文件不存在纔會執行後面的操做

#!/bin/bash
f="/tmp/chamlinux"
[ -f /tmp/chamlinux ] && rm -f $f
若是文件不存在
#!/bin/bash
f="/tmp/chamlinux"
if [ ! -f $f ]
then 
    touch $f
fi

20.7 if特殊用法

•if [ -z "$a" ]  這個表示當變量a的值爲空時會怎麼樣,

#!/bin/bash
a=`wc -l /etc/passwd`
if [ -z "$a" ]
then
echo error
exit
elif [ $a -gt 100 ]
then
echo succes 
fi

注意: 在該表達式中引用變量時要用雙引號引發來。

•if [ -n "$a" ] 表示當變量a的值不爲空

#!/bin/bash
a=`wc -l /etc/passwd`
if [ -n "$a" ]
then
echo error
exit
elif [ $a -gt 100 ]
then
echo succes 
fi

•if grep -q '123' 1.txt; then  表示若是1.txt中含有'123'的行時會怎麼樣

#!/bin/bash
if grep -q 'lxy123' /etc/passwd
then
echo "succes exist" 
exit
elif  useradd lxy123
then
echo succes 
fi

20.8 case判斷 

格式:

case 變量名 in
    value1)
       commond1
       ;;
    value2)
       commod2
       ;;
    value3)
       commod3
       ;;
esac

在case中,能夠在條件中使用「|」,表示或的意思,如:

2|3)
    commond
    ;;

腳本案例:

#!/bin/bash
read -p "Please input a number: " n      //輸入一個數字
if [ -z "$n" ]                           //判斷數字是否存在
then
    echo "Please input a number."        //不存在繼續輸入
    exit 1
fi
n1=`echo $n|sed 's/[0-9]//g'`            //將輸入的數字替換爲空
if [ -n "$n1" ]                          //判斷$n1是否爲空
then
 echo "Please input a number."           
 exit 1
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
case $tag in
    1)
        echo "不及格"
        ;;
    2)
        echo "及格"
        ;;
    3)
        echo "良好"
     ;;
    4)
        echo "優秀"
        ;;
    *)
        echo "The number range is 0-100."
        ;;
esac
相關文章
相關標籤/搜索