天天看點

shell腳本中的邏輯判斷 檔案目錄屬性判斷 if特殊用法 case判斷

一、shell腳本中的邏輯判斷

shell腳本中的邏輯判斷 檔案目錄屬性判斷 if特殊用法 case判斷

在shell腳本中,很多都會邏輯判斷,判斷某一個數值,判斷某一個檔案,或者某一個目錄,我們針對判斷結果再做一些操作,如果沒有判斷,就沒法做一些操作

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

例子:

[root@linux-01 ~]# if [ $a -ge 3 ] //分行寫就是這樣寫

then echo ok fi ok [root@linux-01 ~]# if [ $a -ge 3 ]; then echo ok; fi //這是一行寫的格式 //解釋:-gt表示大于的意思,格式比較特殊:方括号内的兩邊必須都要有空格,-gt的兩邊必須都要有空格,條件語句到處都是空格 我們可以把上面的這條指令寫成腳本: [root@linux-01 ~]# cd shell/ [root@linux-01 shell]# vi if1.sh #!/bin/bash a=5 if [ $a -gt 3 ] [root@linux-01 shell]# sh if1.sh //執行腳本,檢視腳本執行結果 大部分的腳本都使用這個格式,都使用邏輯判斷if ,then

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

[root@linux-01 shell]# cp if1.sh if2.sh

[root@linux-01 shell]# vi if2.sh

a=1

else

echo nook

[root@linux-01 shell]# sh if2.sh //執行增加了else語句的判斷,輸出nook

nook

[root@linux-01 shell]# sh -x if2.sh //-x可以檢視腳本的詳細執行過程

'[' 1 -gt 3 ']' //1和3比較不是大于3的,判斷是else

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

[root@linux-01 shell]# cp if2.sh if3.sh

[root@linux-01 shell]# vi if3.sh

if [ $a -gt 1 ] //如果a大于1

echo ">1" //輸出>1

elif [ $a -lt 6 ] //在大于1的基礎上小于6,elif這個條件判斷可以寫多次

echo "<6 && >1" //輸出<6 && >1

else //else表示除了上面兩個條件之外的其他的條件了

[root@linux-01 shell]# sh -x if3.sh //使用-x選項檢視腳本詳細執行過程

'[' 5 -gt 1 ']'

echo '>1'

1 上面這個if3.sh這個腳本的邏輯可能有問題,需要改進 邏輯判斷表達式: if [ $a -gt $b ] 表示如果a>b; if [ $a -lt 5 ]表示如果a<5; if [ $b -eq 10 ]表示如果b=10; if [ $b -ne 10 ]表示如果b不=10; -gt(>) -lt(<) -ge(>=) -le(<=) -eq(==) -ne(!=) 如果想直接使用大于号小于号,可以換種格式去寫 [root@linux-01 shell]# a=3 [root@linux-01 shell]# if (($a>1)); then echo ok; fi //使用兩個括号,但是使用起來繁瑣 可以使用&& || 結合多個條件,&&表示并且,||表示或者 if [ $a -gt 5 ] && [ $a -lt 10 ]; then //如果a大于5并且小于10,then if [ $b -gt 5 ] || [ $b -lt 3 ]; then //如果b大于5或者小于3,then

二、檔案目錄屬性判斷

shell腳本中的邏輯判斷 檔案目錄屬性判斷 if特殊用法 case判斷

1、[ -f file ]判斷檔案是否是普通檔案,并且存在

執行個體:建立file1.sh腳本

[root@linux-01 shell]# vi file1.sh

f="/tmp/aminglinux"

if [ -f $f ]

echo $f exist

touch $f

[root@linux-01 shell]# sh -x file1.sh //檢視腳本執行過程

f=/tmp/aminglinux //檢視檔案路徑

'[' -f /tmp/aminglinux ']' //去比較檔案存不存在

touch /tmp/aminglinux //不存在就去建立

[root@linux-01 shell]# sh -x file1.sh //再次執行腳本提示檔案已經存在

f=/tmp/aminglinux

'[' -f /tmp/aminglinux ']'

echo /tmp/aminglinux exist

/tmp/aminglinux exist

2、[ -d file ]判斷檔案是否是目錄,并且存在

執行個體:

[root@linux-01 shell]# cp file1.sh file2.sh //拷貝下file1,sh腳本去修改

[root@linux-01 shell]# vi file2.sh

if [ -d $f ] //這裡修改為-d,判斷是不是

[root@linux-01 shell]# sh -x file2.sh

'[' -d /tmp/aminglinux ']' //比較不是目錄

touch /tmp/aminglinux //建立目錄

3、[ -e file ]判斷檔案或目錄是否存在

執行個體;

if [ -e $f ] //-e判斷它不管是檔案或者目錄

'[' -e /tmp/aminglinux ']'

//目錄和檔案都是可以touch的,如果touch的時候這個檔案或者目錄是存在的,那麼可以更改檔案的三個time,分别是atime,ctime,mtime

4、[ -r file ]判斷檔案是否可讀

if [ -r $f ]

echo $f readable

[root@linux-01 shell]# sh file2.sh //執行腳本,提示可讀

/tmp/aminglinux readable

5、[ -w file ]判斷檔案是否可寫

if [ -w $f ]

echo $f writeable

[root@linux-01 shell]# sh file2.sh ////執行腳本,提示可寫

/tmp/aminglinux writeable

那它判斷可讀可寫實際上是判斷執行這個shell腳本的目前使用者,我們是以root的身份去執行的腳本

6、 [ -x file ]判斷檔案是否可執行

if [ -x $f ]

echo $f exeable

[root@linux-01 shell]# sh file2.sh //因為沒有執行的權限,我們的腳本也沒有定義else,是以沒有任何輸出資訊

下面對這個腳本進行改良

在工作中使用最多的是:通常先判斷檔案存在不存在,如果存在,就會删除這個檔案,&&表示目前面的指令執行成功以後才會去執行後面的指令

那麼[ -f $f ] && rm -f $f這一行指令等同于下面這四行指令

rm -f $f

[ -f $f ] || touch $f 表示如果$f檔案執行不成功的時候,才執行後面的touch $f指令

[ -f $f ] || touch $f 等同于下面這四行

if [ ! -f $f ]

三、if特殊用法

shell腳本中的邏輯判斷 檔案目錄屬性判斷 if特殊用法 case判斷

1、if [ -z "$a" ] 這個表示當變量a的值為空時會怎麼樣

[root@linux-01 shell]# vi if4.sh

n=<code>wc -l /tmp/lalal</code>

if [ -z "$n" ]

echo error

exit

elif [ $n -gt 100 ]

echo abcdef

//if [ -z "$n" ]表示當變量n的值為空的時候

[root@linux-01 shell]# sh -x if4.sh //執行腳本,檢視執行過程

++ wc -l /tmp/lalal

wc: /tmp/lalal: no such file or directory

n=

'[' -z '' ']'

error

對if4.sh腳本再次完善:

if [ ! -f /tmp/lalal ]

echo "/tmp/lalal not exist."

[root@linux-01 shell]# sh if4.sh //執行腳本,如果/tmp/lalal檔案不存在,就直接退出腳本

/tmp/lalal not exist.

2、if [ -n "$a" ] 表示當變量a的值不為空

[root@linux-01 shell]# if [ -n 01.sh ]; then echo ok; fi //如果01.sh不為空,輸出ok

[root@linux-01 shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi

b is null

//如果變量b值不為空,輸出$b,否則輸出"b is null"

注意,如果[ -n 01.sh ]的是檔案,不需要使用引号,如果 [ -n "$b" ]中的是變量,需要使用引号引起來

3、if grep -q '123' 1.txt; then 表示如果1.txt中含有‘123’的行時怎麼樣

需求:在腳本中,可以使用一個指令的結果作為判斷條件,判斷某一個檔案中是否含有某一個字元串,比如判斷系統中的使用者裡面是否有user1這個使用者,我們可以使用如下指令去判斷

[root@linux-01 shell]# grep -w 'user1' /etc/passwd //-w選項可以更加精準的去比對

user1:x:1000:1000::/home/user1:/bin/bash

有了這樣的思路,可以這樣去判斷

[root@linux-01 shell]# if grep -wq 'user1' /etc/passwd; then echo "user1 exist"; fi //-q選項可以不顯示過濾掉的内容

user1 exist

4、if [ ! -e file ]; then 表示檔案不存在時會怎麼樣

5、if (($a&lt;1)); then... 等同于 if [ $a -lt 1 ]; then...

6、[ ] 中不能使用&lt;,&gt;,==,!=,&gt;=,&lt;=這樣的符号

四、case判斷

shell腳本中的邏輯判斷 檔案目錄屬性判斷 if特殊用法 case判斷

雙分号表示第一個判斷結束,進入下一個判斷

shell腳本執行個體:

[root@linux-01 shell]# vi case.sh

read -p "please input a number: " n //請使用者輸入一個數字,n作為一個捕獲的變量使用者輸入什麼輸出什麼值

if [ -z "$n" ] //如果n的值為空,請輸入一個數字

echo "please input a number."

exit 1 //退出來捕獲變量1的值

n1=<code>echo $n|sed 's/[0-9]//g'</code> //判斷輸入的數字是否含有字母或全是字母,使用sed把所有數字置空

if [ ! -z $n1 ] //這一行或者使用if [ -n "$n1" ]代替,如果判斷不為空,退出來,提示使用者輸入一個數字

exit 1

fi //如果為空,說明輸入的合法,繼續執行下面的腳本内容

if [ $n -lt 60 ] &amp;&amp; [ $n -ge 0 ] //如果是數字,需要判斷數字的範圍

tag=1

elif [ $n -ge 60 ] &amp;&amp; [ $n -lt 80 ]

tag=2

elif [ $n -ge 80 ] &amp;&amp; [ $n -lt 90 ]

tag=3

elif [ $n -ge 90 ] &amp;&amp; [ $n -le 100 ]

tag=4

tag=0

case $tag in

1)

echo "not ok"

;;

2)

echo "ok"

3)

echo "ook"

4)

echo "oook"

*)

echo "the number range is 0-100."

esac