1. 程式人生 > >Shell 命令詳解之 if 命令

Shell 命令詳解之 if 命令

Shell 命令詳解之 if 命令

1. 常用引數

-eq:等於[equal]
-n : 字串是否不為空
-ne:不等於[not equal]
-le:小於等於[less and equal]
-ge:大於等於[greater and equal]
-lt:小於[less than]
-gt:大於[greater than]
-a: 與 [and]
-o:或 [or]
!:非

  • example 1 -eq
    指令碼如下:
[[email protected] shells]# cat test1.sh 
#!/bin/bash
a=$1
if [ $a
-eq 1 ] then echo a = 1 else echo a != 1 fi

執行結果:

[[email protected] shells]# ./test1.sh 1
a = 1
[[email protected]erver4 shells]# ./test1.sh 2
a != 1
  • example 2 -n
[[email protected] shells]# cat test1.sh 
#!/bin/bash
a=$1
if [ -n "$a" ];
then 
 echo a isn"'"t empty string
else
echo a is empty string fi
[[email protected] shells]# ./test1.sh 
a is empty string
[[email protected] shells]# ./test1.sh hadoop
a isn't empty string