天天看點

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​

    ​ 腳本如下:
[root@server4 shells]# cat test1.sh 
#!/bin/bash
a=$1
if [ $a -eq 1 ]
then 
 echo a = 1
else
 echo a != 1
fi      
[root@server4 shells]# ./test1.sh 1
a = 1
[root@server4 shells]# ./test1.sh 2
a != 1      
  • example 2 ​

    ​-n​

[root@server4 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      
[root@server4 shells]# ./test1.sh 
a is empty string
[root@server4 shells]# ./test1.sh hadoop
a isn't empty string