語法
一、格式1
if 條件 ; then 語句; fi
例:若是a大於3,打印OKshell
[root@a ~]# vi 2.sh [root@a ~]# bash -v 2.sh #!/bin/bash a=5 if [ $a -gt 3 ] then echo ok fi ok [root@a ~]# bash 2.sh ok [root@a ~]#
二、格式2
if 條件; then 語句; else 語句; fi
例:若是a小於3,則打印no okbash
[root@a ~]# bash -v 3.sh #!/bin/bash a=2 if [ $a -gt 3 ] then echo ok else echo no ok fi no ok [root@a ~]# bash 3.sh no ok [root@a ~]#
三、格式3
if …; then … ;elif …; then …; else …; fi
例:ide
[root@a ~]# sh -x 4.sh + a=3 + '[' 3 -gt 4 ']' + '[' 3 -lt 6 ']' + echo '<6 && >1' <6 && >1 [root@a ~]# sh -v 4.sh #!/bin/bash a=3 if [ $a -gt 4 ] then echo ">1" elif [ $a -lt 6 ] then echo "<6 && >1" else echo no ok fi <6 && >1 [root@a ~]#
邏輯判斷表達式
if [ $a -gt $b ]; if [ $a -lt 5 ]; if [ $b -eq 10 ]等
-gt (>); 大於
-lt(<); 小於
-ge(>=); 大於等於
-le(<=);小於等於
-eq(==)等於
; -ne(!=)
注意處處都是空格
能夠使用 && || 結合多個條件
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [ $b -lt 3 ]; then3d
一、if 判斷文件、目錄屬性
[ -f file ]判斷是不是普通文件,且存在
例:若是riven文件不存在,就建立code
#!/bin/bash c="/tmp/riven" if [ -f $c ] then echo $c exist else touch $c fi
[ -d file ] 判斷是不是目錄,且存在blog
#!/bin/bash c="/tmp/2018" if [ -d $c ] then echo $c exist else touch $c fi
[ -e file ] 判斷文件或目錄是否存在字符串
#!/bin/bash c="/tmp/2018" if [ -e $c ] then echo $c exist else touch $c fi
[ -r file ] 判斷文件是否可讀input
#!/bin/bash c="/tmp/2018" if [ -r $c ] then echo $c readable else touch $c fi
[ -w file ] 判斷文件是否可寫it
#!/bin/bash c="/tmp/2018" if [ -w $c ] then echo $c writeable else touch $c fi
[ -x file ] 判斷文件是否可執行class
#!/bin/bash c="/tmp/2018" if [ -x $c ] then echo $c exeable else touch $c fi
由於文件不可執行,全部沒有任何輸出。
二、腳本縮寫
#!/bin/bash c="/tmp/2018" [ -f $f ] && rm -f $f #上面的命令等同於下面的命令 if [-f $f ] then rm -f $f fi
#!/bin/bash c="/tmp/2018" [ -f $f ] || touch $f #若是這個文件不存在就執行後面的文件 #上面的命令等同於下面的命令 if [ !-f $f ] #歎號表示取反 then touch $f fi
一、if [ -z "$a" ] 這個表示當變量a的值爲空時會出現錯誤
#!/bin/bash if [ ! -f /tmp/a1 ] then echo "/tmp/a1 not exist." exit n=`wc -l /tmp/a1` if [ -z "$n" ] then echo error exit else if [ $n -gt 100 ] then echo ok fi
if [ -n "$a" ] 表示當變量a的值不爲空
[root@a ~]# if [ -n 2.sh ]; then echo ok; fi ok [root@a ~]# if [ -n "$a" ]; then echo ok; else echo "a is null";fi ok [root@a ~]# if [ -n "$aa" ]; then echo ok; else echo "a is null";fi a is null [root@a ~]# if [ -n "$aa" ]; then echo ok; else echo "aa is null";fi aa is null [root@a ~]#
if grep -q '123' 1.txt; then 表示若是1.txt中含有'123'的行時會怎麼樣?
[root@a ~]# grep -w 'root' /etc/passwd root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin [root@a ~]# if grep -w 'root' /etc/passwd; then echo "root exist"; fi root:x:0:0:root:/root:/bin/bash operator:x:11:0:operator:/root:/sbin/nologin root exist [root@a ~]# if grep -wq 'root' /etc/passwd; then echo "root exist"; fi #-q不顯示過濾內容 root exist [root@a ~]# if ! grep -w 'user1' /etc/passwd;then useradd user1;fi # !取反
if [ ! -e file ]; then 表示文件不存在時會怎麼樣
if (($a<1)); then …等同於 if [ $a -lt 1 ]; then…
[ ] 中不能使用<,>,==,!=,>=,<=這樣的符號
1、shell中的case判斷
格式:
case 量名 in value1) command ;; value2) command ;; *) commond ;; esac
在case程序中,能夠在條件中使用|,表示或的意思好比
2|3)
command
;;
二、腳本案例
#!/bin/bash read -p "Please input a number: " n #read命令讓用戶輸入字符串,n表示要捕獲的變量。 if [ -z "$n" ] then echo "Please input a number." exit 1 fi n1=`echo $n|sed 's/[0-9]//g'` # 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