#if 表示中文的 若是 ;格式1:if條件; then 語句; fi [root@qingyun-01 ~]# a=5;if [ $a -gt 3 ];then echo ok; fi ok ;參考文本格式以下: #!/bin/bash a=5 if [ $a -gt 3 ] #-gt 表示 >= then echo ok fi ;格式2:if條件; then 語句; else 語句; fi [root@qingyun-01 shell]# a=5;if [ $a -gt 6 ];then echo ok;else echo no ok;fi no ok ;參考文本格式以下: #!/bin/bash a=5 if [ $a -gt 6 ] then echo ok else echo no ok fi ;格式3:if...; then ...; elif ...; then ...; else ...; fi [root@qingyun-01 shell]# a=5;if [ $a -gt 10 ];then echo 0k;elif [ $a -gt 6 ];then echo $a\>6;else echo $a\<6;fi 5<6 ;參考文本格式以下: [root@qingyun-01 shell]# sh -x if3.sh + a=5 + '[' 5 -gt 10 ']' + '[' 5 -gt 6 ']' + echo '5<6' 5<6 [root@qingyun-01 shell]# cat if3.sh #!/bin/bash a=5 if [ $a -gt 10 ] then echo ">10" elif [ $a -gt 6 ] then echo $a">6" else echo $a"<6" fi
邏輯判斷吧表達式:
if [ $a -gt $b ];
if [ $a -lt 5 ];
if [ $b -eq 10 ] 等
-gt(>); -lt(<); -ge(>=);
-le(<=); -eq(==); -ne(!=)
注意處處都是空格shell
#另外一種寫法: [root@qingyun-01 shell]# a=5;if (($a>10));then echo OK;else echo NO;fi NO [root@qingyun-01 shell]# a=5;if (($a>=5));then echo OK;else echo NO;fi OK [root@qingyun-01 shell]# a=5;if (($a>10));then echo OK;elif (($a>6));then echo $a">6";else echo $a"<6";fi 5<6
if [ $a -gt 5 ] && [ $a -lt 10 ]; then
if [ $b -gt 5 ] || [[ $b -lt 3 ]; thenbash
[root@qingyun-01 shell]# cat file.sh #!/bin/bash f="/tmp/taoyuan" if [ -f $f ] then echo $f exist else touch $f fi ;沒有文件,建立文件 [root@qingyun-01 shell]# sh -x file.sh + f=/tmp/taoyuan + '[' -f /tmp/taoyuan ']' + touch /tmp/taoyuan ;文件已經存在 [root@qingyun-01 shell]# sh -x file.sh + f=/tmp/taoyuan + '[' -f /tmp/taoyuan ']' + echo /tmp/taoyuan exist /tmp/taoyuan exist
[root@qingyun-01 shell]# cat file1.sh #!/bin/bash f="/tmp/taoyuan" if [ -d $f ] then echo $f exist else mkdir $f fi [root@qingyun-01 shell]# sh -x file1.sh + f=/tmp/taoyuan + '[' -d /tmp/taoyuan ']' + mkdir /tmp/taoyuan
[root@qingyun-01 shell]# cat file2.sh #!/bin/bash f="/tmp/taoyuan" if [ -e $f ] then echo $f exist else mkdir $f fi [root@qingyun-01 shell]# sh -x file2.sh + f=/tmp/taoyuan + '[' -e /tmp/taoyuan ']' + echo /tmp/taoyuan exist /tmp/taoyuan exist
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -r $f ];then echo $f read;else echo file reservation;fi /tmp/taoyuan read
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -w $f ];then echo $f write;else echo $f File unable to write;fi /tmp/taoyuan write
[root@qingyun-01 shell]# f="/tmp/taoyuan";if [ -x $f ];then echo $f x file;else echo $f File no execution;fi /tmp/taoyuan x file
[root@qingyun-01 shell]# cat if.sh #!/bin/bash f="/tmp/lalal" if [ ! -f $f ] then echo "$f not exist." exit fi n=`wc -l $f` if [ -z "$a" ] #變量須要雙引號 then echo error exit elif [ $n -gt 100 ] then echo OK fi [root@qingyun-01 shell]# sh if.sh /tmp/lalal not exist.
[root@qingyun-01 shell]# if [ -n if.sh ]; then echo ok;fi ok #能夠判斷一個文件是否爲空 [root@qingyun-01 shell]# if [ -n "$b" ]; then echo $b;else echo "b is null";fi b is null
[root@qingyun-01 shell]# if grep -w 'user' /etc/passwd; then echo "user exist"; fi user:x:1000:1000:user:/home/user:/bin/bash user exist [root@qingyun-01 shell]# if grep -wq 'user' /etc/passwd; then echo "user exist"; fi user exist #grep -q 不顯示過濾的結果
[root@qingyun-01 shell]# cat case.sh #!/bin/bash read -p "Please enter the score:" n if [ -z "$n" ] then echo "For the input score." exit 1 fi n1=`echo $n|sed 's/[0-9]//g'` if [ ! -z "$n1" ] then echo "Non-numeric input." 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 "C" ;; 2) echo "B" ;; 3) echo "A" ;; 4) echo "A+" ;; *) echo "Please enter a scale of 0-100." ;; esac