您现在的位置是:网站首页> 编程资料编程资料
Shell脚本if else语句小结_linux shell_
2023-05-26
448人已围观
简介 Shell脚本if else语句小结_linux shell_
和Java、PHP等语言不一样,sh的流程控制不可为空,如:
复制代码 代码如下:
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脚本test命令使用总结和实例_linux shell_
- Shell脚本echo指令使用小技巧_linux shell_
- Shell脚本数组操作小结_linux shell_
- Shell脚本字符串单引号和双引号的区别浅析_linux shell_
- Shell脚本注释写法_linux shell_
- Shell脚本定义变量和重新赋值_linux shell_
- 什么是Shell?Shell脚本基础知识详细介绍_linux shell_
- 一键配置CentOS iptables防火墙的Shell脚本分享_linux shell_
- 简单的远程FTP定时备份Shell脚本分享_linux shell_
- Shell脚本实现自动修改IP地址_linux shell_
