Shell脚本if else语句小结
2018-10-12 14:09
531 查看
和Java、PHP等语言不一样,sh的流程控制不可为空,如:
<?php
if (isset($_GET["q"])) {
search(q);
}
else {
//do nothing
}
?>
在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样:
if condition
then
command1
command2
...
commandN
fi
当然,也可以写成一行(适用于终端命令提示符),像这样:
if test $[2*3] -eq $[1+5]; then echo 'The two numbers are equal!'; fi;
末尾的fi就是if倒过来拼写,后面还会遇到类似的。
if else格式
if condition
then
command1
command2
...
commandN
else
command
fi
if else-if else格式
if condition1
then
command1
elif condition2
command2
else
commandN
fi
if else语句经常与test命令结合使用,如下所示:
num1=$[2*3]
num2=$[1+5]
if test $[num1] -eq $[num2]
then
echo 'The two numbers are equal!'
else
echo 'The two numbers are not equal!'
fi
输出:
The two numbers are equal!
您可能感兴趣的文章:
相关文章推荐
- Shell脚本if else语句小结
- 【Shell脚本学习16】Shell if else语句
- shell脚本编程:条件判断if语句使用小结
- 续写vim,shell脚本基础编辑,read命令,if与case判断语句,文件查找方式,压缩与解压,
- shell脚本之if语句
- shell脚本之while for if 语句和编写计划任务
- Shell if else语句
- Shell 脚本基础 - 使用 if 语句进行条件检测
- 30分钟玩转Shell-Shell if else语句
- shell脚本之-if流程控制语句和for循环语句的使用
- Shell脚本语法--if/then/elif/else/fi
- Shell if else语句
- 第五章 shell 机构化脚本【if-else-fi】
- shell脚本中if流程控制语句的应用
- Shell中的if else语句小演示
- Shell脚本入门(六) - 条件控制语句: if
- Shell if语句用法小结
- shell脚本之if语句
- shell脚本if判断语句报错[: too many arguments的两种原因
- linux之基础shell脚本编程2 if语句循环判断