Shell 命令详解之 if 命令

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/84395821

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

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/84395821