shell脚本嵌套if else
来源:互联网 发布:浙江卫视直播软件 编辑:程序博客网 时间:2024/05/21 04:21
尊重原创
http://blog.csdn.net/huangjin0507/article/details/45048975
【注意1】:和Java、PHP等语言不一样,sh的流程控制不可为空,如:
在sh/bash里可不能这么写,如果else分支没有语句执行,就不要写这个else,就像这样:
当然,也可以写成一行(适用于终端命令提示符),像这样:
上面这个脚本,执行结果是:
max is a
1、if格式
if [ condition ] --注意括号两边有空格,condition 是个条件表达式
then
commands
fi
作用:判断 condition 条件是否成立,如果成立,执行中间的命令 commands,不成立不执行。
如: if [ $a -gt $b ]
then
echo "a大于b"
fi
if 可以接 条件表达式 (如 if [ $a -gt $b ]),也可以直接接一个命令(如 if mkdir /abc ) ,这时,会把命令的执行结果作为判断,如果成功执行,就相当于条件成立,如果执行不成功,就相当于条件不成立。
2、if else格式
3、if else-if else格式
if else语句经常与test命令结合使用,如下所示:
输出:
The two numbers are equal!
4、if的嵌套
格式一:
if [ condition ]
then
if [ condition ]
then
commands1
else
commands2
fi
fi
格式二:
if [ condition ]
then
if [ condition ]
then
commands1
else
commands2
fi
else
commands3
fi
5、多条件表示:
逻辑与
if [ condition1 -a condition2 ]
或 if [ condition1 ] && [ condition2 ]
逻辑或
if [ condition1 -o condition2 ]
或 if [ condition1 ] || [ condition2 ]
逻辑非(取反)
!
- shell脚本嵌套if else
- shell 脚本 if elseif else
- shell 脚本 if elseif else
- shell 脚本中的if else
- 【脚本】shell中的if else if
- shell 脚本 if then else fi
- shell脚本学习之if..else用法
- shell 脚本-case和if else用法
- if...else嵌套
- 【Shell脚本学习16】Shell if else语句
- if , if else , if else if , if的嵌套
- Shell脚本语法-- if/then/elif/else/fi
- 第五章 shell 机构化脚本【if-else-fi】
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法-- if/then/elif/else/fi
- Shell脚本语法--if/then/elif/else/fi
- Linux--Shell脚本(二)条件判断之if else
- shell if else
- 协议
- dql数据权限控制
- HTML5新增语义元素
- Linux userdel 和 deluser 的区别
- Qml文件的两种加载方式
- shell脚本嵌套if else
- python+selenium初级(8)—提示框处理
- 根据 Google-speed 的优化网页速度
- 2017 Google 开发者大会全记录
- MySQL必知必会
- cglib初步使用代理
- Linux useradd 和 adduser 的区别
- Android中级篇之Fresco-加载图片基础[详细图解Fresco的使用]
- JS的解析与执行——函数预处理与执行