1. 程式人生 > >shell腳本中的邏輯判斷 文件目錄屬性判斷 if特殊用法 case判斷

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

文件路徑 erro 符號 put 可執行 oot 工作 命令 空格

一、shell腳本中的邏輯判斷
技術分享圖片
在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 ]
then
echo ok
fi
[root@linux-01 shell]# sh if1.sh //執行腳本,查看腳本執行結果
ok
大部分的腳本都使用這個格式,都使用邏輯判斷if ,then

格式2:if條件;then語句;else語句;fi
例子:
[root@linux-01 shell]# cp if1.sh if2.sh
[root@linux-01 shell]# vi if2.sh

#!/bin/bash
a=1
if [ $a -gt 3 ]
then
echo ok
else
echo nook
fi
[root@linux-01 shell]# sh if2.sh //執行增加了else語句的判斷,輸出nook
nook
[root@linux-01 shell]# sh -x if2.sh //-x可以查看腳本的詳細執行過程

  • a=1
  • ‘[‘ 1 -gt 3 ‘]‘ //1和3比較不是大於3的,判斷是else
  • echo nook
    nook

格式3:if...;then...;elif...;then...;else...;fi
例子:
[root@linux-01 shell]# cp if2.sh if3.sh

[root@linux-01 shell]# vi if3.sh
#!/bin/bash
a=5
if [ $a -gt 1 ] //如果a大於1
then
echo ">1" //輸出>1
elif [ $a -lt 6 ] //在大於1的基礎上小於6,elif這個條件判斷可以寫多次
then
echo "<6 && >1" //輸出<6 && >1
else //else表示除了上面兩個條件之外的其他的條件了
echo nook
fi
[root@linux-01 shell]# sh -x if3.sh //使用-x選項查看腳本詳細執行過程

  • a=5
  • ‘[‘ 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 //使用兩個括號,但是使用起來繁瑣
    ok
    可以使用&& || 結合多個條件,&&表示並且,||表示或者
    if [ $a -gt 5 ] && [ $a -lt 10 ]; then //如果a大於5並且小於10,then
    if [ $b -gt 5 ] || [ $b -lt 3 ]; then //如果b大於5或者小於3,then

二、文件目錄屬性判斷
技術分享圖片
1、[ -f file ]判斷文件是否是普通文件,並且存在
實例:創建file1.sh腳本
[root@linux-01 shell]# vi file1.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -f $f ]
then
echo $f exist
else
touch $f
fi
[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
#!/bin/bash
f="/tmp/aminglinux"
if [ -d $f ] //這裏修改為-d,判斷是不是
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# sh -x file2.sh

  • f=/tmp/aminglinux
  • ‘[‘ -d /tmp/aminglinux ‘]‘ //比較不是目錄
  • touch /tmp/aminglinux //創建目錄

3、[ -e file ]判斷文件或目錄是否存在
實例;
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -e $f ] //-e判斷它不管是文件或者目錄
then
echo $f exist
else
touch $f
fi
[root@linux-01 shell]# sh -x file2.sh

  • f=/tmp/aminglinux
  • ‘[‘ -e /tmp/aminglinux ‘]‘
  • echo /tmp/aminglinux exist
    /tmp/aminglinux exist
    //目錄和文件都是可以touch的,如果touch的時候這個文件或者目錄是存在的,那麽可以更改文件的三個time,分別是atime,ctime,mtime

4、[ -r file ]判斷文件是否可讀
實例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -r $f ]
then
echo $f readable
fi
[root@linux-01 shell]# sh file2.sh //執行腳本,提示可讀
/tmp/aminglinux readable

5、[ -w file ]判斷文件是否可寫
實例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -w $f ]
then
echo $f writeable
fi
[root@linux-01 shell]# sh file2.sh ////執行腳本,提示可寫
/tmp/aminglinux writeable
那它判斷可讀可寫實際上是判斷執行這個shell腳本的當前用戶,我們是以root的身份去執行的腳本

6、 [ -x file ]判斷文件是否可執行
實例:
[root@linux-01 shell]# vi file2.sh
#!/bin/bash
f="/tmp/aminglinux"
if [ -x $f ]
then
echo $f exeable
fi
[root@linux-01 shell]# sh file2.sh //因為沒有執行的權限,我們的腳本也沒有定義else,所以沒有任何輸出信息

下面對這個腳本進行改良
在工作中使用最多的是:通常先判斷文件存在不存在,如果存在,就會刪除這個文件,&&表示當前面的命令執行成功以後才會去執行後面的命令
那麽[ -f $f ] && rm -f $f這一行命令等同於下面這四行命令
if [ -f $f ]
then
rm -f $f
fi
[ -f $f ] || touch $f 表示如果$f文件執行不成功的時候,才執行後面的touch $f命令
[ -f $f ] || touch $f 等同於下面這四行
if [ ! -f $f ]
then
touch $f
fi

三、if特殊用法
技術分享圖片
1、if [ -z "$a" ] 這個表示當變量a的值為空時會怎麽樣
例子:
[root@linux-01 shell]# vi if4.sh
#!/bin/bash
n=wc -l /tmp/lalal
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo abcdef
fi
//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 ‘‘ ‘]‘
  • echo error
    error
  • exit

對if4.sh腳本再次完善:
[root@linux-01 shell]# vi if4.sh
#!/bin/bash
if [ ! -f /tmp/lalal ]
then
echo "/tmp/lalal not exist."
exit
fi
n=wc -l /tmp/lalal
if [ -z "$n" ]
then
echo error
exit
elif [ $n -gt 100 ]
then
echo abcdef
fi
[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
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<1)); then... 等同於 if [ $a -lt 1 ]; then...
6、[ ] 中不能使用<,>,==,!=,>=,<=這樣的符號

四、case判斷
技術分享圖片
雙分號表示第一個判斷結束,進入下一個判斷
Shell腳本實例:
[root@linux-01 shell]# vi case.sh
#!/bin/bash
read -p "Please input a number: " n //請用戶輸入一個數字,n作為一個捕獲的變量用戶輸入什麽輸出什麽值
if [ -z "$n" ] //如果n的值為空,請輸入一個數字
then
echo "Please input a number."
exit 1 //退出來捕獲變量1的值
fi

n1=echo $n|sed ‘s/[0-9]//g‘ //判斷輸入的數字是否含有字母或全是字母,使用sed把所有數字置空
if [ ! -z $n1 ] //這一行或者使用if [ -n "$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 "not ok"
;;
2)
echo "ok"
;;
3)
echo "ook"
;;
4)
echo "oook"
;;
*)
echo "The number range is 0-100."
;;
esac

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