if语句格式
if command;then
command;
else
command;
fi
示例
#!/bin/bash
if grep "root" /etc/passwd;then
echo "true";
else
echo "false";
fi;
if 的三种条件表达式
格式 |
说明 |
if command;then
if function;then |
命令执行成功,等于返回0 (比grep ,找到匹配) 执行失败,返回非0 (grep,没找到匹配) |
if [ expression_r_r_r ];then |
表达式结果为真,则返回0,if把0值引向then |
if test expression_r_r_r ;then |
表达式结果为真,则返回0,if把0值引向then |
#!/bin/bash
# 输出false
if test 1 < 2 ;then
echo "true";
else
echo "false";
fi;
# 输出false
if [ 1 < 2 ];then
echo "true";
else
echo "false";
fi;
[ ] && ——快捷if
格式 |
说明 |
[ expression_r_r_r ] && command |
&& 可以理解为then,如果左边的表达式为真则执行右边的语句 |
[ -f /etc/passwd ] && echo "exists";
exists
shell的if与c语言if的功能上的区别
shell if |
c语言if |
0为真,走then |
正好相反,非0走then |
不支持整数变量直接if 必须:if [ i –ne 0 ] 但支持字符串变量直接if if [ str ] 如果字符串非0 |
支持变量直接if if (i ) |
#!/bin/bash
# 输出true
if [ 0 ];then
echo "true";
else
echo "false";
fi;
# 输出true
if [ 1 ];then
echo "true";
else
echo "false";
fi;
以command作为if 条件
以多条 command 作为条件
#!/bin/bash
echo -n "input:";
read user;
# 多条指令,这些命令之间相当于“and”(与)
if grep ${user} /etc/passwd > /tmp/null;who -u | grep $user;then
echo "${user} has logged";
else
echo "${user} has not logged";
fi;
执行结果
[root@jin test]# bash ../test.sh
input:root
root pts/1 2016-11-12 09:22 . 5577 (192.168.1.62)
root has logged
[root@jin test]# bash ../test.sh
input:jin
jin has not logged
以函数作为if条件 (函数就相当于command,函数的优点是其return值可以自定义)
#!/bin/bash
function yesorno()
{
return 1;
}
# 以函数作为if条件
if yesorno;then
echo "return is 0,true";
else
echo "return is not 0,false";
fi;
执行结果
bash ../test.sh
return is not 0,false
if command 等价于 command+if $?
#!/bin/bash
echo -n "input:";
read user;
# 多条指令,这些命令之间相当于“and”(与)
grep ${user} /etc/passwd > /tmp/null;who -u | grep $user;
# $? 其实是上次命令的返回值
if [ $? -eq 0 ];then
echo "${user} has logged";
else
echo "${user} has not logged";
fi;
以条件表达式作为 if条件
传统if 从句子——以条件表达式作为 if条件
if [ 条件表达式 ]
then
command
command
command
else
command
command
fi
条件表达式
- 文件表达式
|表达式|说明|
|:-:|:-|
|if [ -f file ]| 如果文件存在|
|if [ -d ... ]| 如果目录存在|
|if [ -s file ]| 如果文件存在且非空 |
|if [ -r file ]| 如果文件存在且可读|
|if [ -w file ]| 如果文件存在且可写|
|if [ -x file ]| 如果文件存在且可执行 |
- 整数变量表达式
|表达式|说明|
|:-:|:-|
|if [ int1 -eq int2 ]| 如果int1等于int2 |
|if [ int1 -ne int2 ]| 如果不等于 |
|if [ int1 -ge int2 ]| 如果>=|
|if [ int1 -gt int2 ]| 如果>|
|if [ int1 -le int2 ]| 如果<=|
|if [ int1 -lt int2 ]| 如果<|
- 字符串变量表达式
表达式 |
说明 |
If [ \$a = \$b ] |
如果a等于b 字符串允许使用赋值号做等号 |
if [ $string1 != $string2 ] |
如果string1不等于string2 |
if [ -n $string ] |
如果string 非空(非0),返回0(true) |
if [ -z $string ] |
如果string 为空 |
if [ $sting ] |
如果string 非空,返回0 (和-n类似) |
条件表达式引用变量要带$
a=a;
b=a;
# 输出 false,因为字母a不等于字母b
if [ a = b ];then
echo "true";
else
echo "false";
fi;
a=a;
b=a;
# 输出 true,因为变量a等于变量b
if [ $a = $b ];then
echo "true";
else
echo "false";
fi;
-eq -ne -lt -nt只能用于整数,不适用于字符串,字符串等于用赋值号=
#!/bin/bash
if [ "aa" -eq "aa" ];then
echo "true";
else
echo "false";
fi;
执行结果
../test.sh: line 2: [: aa: integer expression expected
false
=放在别的地方是赋值,放在if [ ] 里就是字符串等于,shell里面没有==的,那是c语言的等于
无空格的字符串,可以加" ",也可以不加
#!/bin/bash
a="aaa";
b=aaa;
# 输出结果为true
if [ $a = $b ];then
echo "true";
else
echo "false";
fi;
= 作为等于时,其两边都必须加空格,否则失效
等号也是操作符,必须和其他变量,关键字,用空格格开 (等号做赋值号时正好相反,两边不能有空格)
#!/bin/bash
a="aaaa";
b="aaa";
# 输出true,因为if把$a=$b连读成一个变量,这个表达式实际是字串 "aaaa=aaa",所以走 then
if [ $a=$b ];then
echo "true";
else
echo "false";
fi
If [ $ANS ] 与 if [ -n $ANS ]
-n会把空格,回车也识别成非空
#!/bin/bash
echo "enter something,please\n";
read str;
#if [ $str ];then
if [ -n $str ];then
echo $str;
else
echo "empty";
fi;
整数条件表达式,大于,小于,shell里没有> 和< ,会被当作尖括号,只有-ge,-gt,-le,lt
#!/bin/bash
echo "enter a number,please\n";
read str;
if [ $str -lt 100 ];then
echo "$str < 100\n";
else
echo "$str >= 100\n";
fi;
逻辑表达式
表达式 |
例子 |
说明 |
if [ ! 表达式 ] |
if [ ! -d $num ] |
如果不存在目录$num |
if [ 表达式1 –a 表达式2 ] |
if [ $str -lt 100 -a $str -gt 10 ] |
如果$str小于100并且大于10 |
if [ 表达式1 –o 表达式2 ] |
if [ $str -lt 100 -a $str -gt 10 ] |
如果$str小于100或者大于10 |
- 表达式与前面的= != -d –f –x -ne -eq -lt等合用
- 逻辑符号就正常的接其他表达式,没有任何括号( ),就是并列
if [ -z "$JHHOME" -a -d $HOME/$num ]
- 注意逻辑与-a与逻辑或-o很容易和其他字符串或文件的运算符号搞混了
以 test 条件表达式 作为if条件
if test $num -eq 0 等价于 if [ $num –eq 0 ]
#!/bin/bash
echo "enter a number,please\n";
read str;
# 下面两种if,效果相同
if test $str -lt 100 -a $str -gt 10 ;then
#if [ $str -lt 100 -a $str -gt 10 ];then
echo "$str < 100\n";
else
echo "$str >= 100\n";
fi;
if简化语句
&& 如果是“前面”,则“后面”
# 变量不为空则输出变量
[ -n $HOME ] && echo $HOME;
|| 如果不是“前面”,则后面
# 如果变量为空,则输出提示
[ -n $HOME ] || echo '$HOME is empty'
用简化 if 和$1,$2,$3来检测参数,不合理就调用help
[ -z "$1" ] && help 如果第一个参数不存在(-z 字符串长度为0 )
[ "$1" = "-h" ] && help 如果第一个参数是-h,就显示help
转载自 http://blog.csdn.net/buutterfly/article/details/6615162