-
在其他语法中常见的三目运算方式:
表达式 ? 表达式 : 表达式 ;
-
在
shell
中也有类似的方式:command1 && command2 || command3
如果
command
是一连串的组合,那么可以使用{ }
将commands
括起来。注意:代码块若用在函数中,{ }
最后一个必须是;
command1 && { command2_1; command2_2; command2_3; } || { command3_1; command3_3; command3_3; }
-
举例
#!/bin/bash res="mp-weixin" iswx=$([[ $res =~ "weixin" ]] && echo 1 || echo 0) echo "$iswx"
# fileName 文件不存在,则退出,就可以按照下面方式执行 [ -e $fileName ] || { echo -e "fileName Not existed!"; exit 1; } #也或者可以增加一些 log 打印信息 [ -e $fileName ] && echo -e "$fileName existed" || { echo -e "$fileName Not existed!"; exit 1; } #多个命令集合的组合 [ -e $fileName ] && echo -e "$fileName existed"; ehco -e "Other Necessary Information" || { echo -e "$fileName Not existed!"; exit 1; } [ -e $fileName ] && { echo -e "$fileName existed"; ehco -e "Other Necessary Information"; } || { echo -e "$fileName Not existed!"; exit 1; } #读取IP地址,若为空,则使用默认IP,否则使用新的IP地址 read -p "Please input Management IP (Default is $DEFAULT_IP): " MGMT_IP [[ -z $MGMT_IP ]] && { MGMT_IP=$DEFAULT_IP; echo -e "Using default IP $MGMT_IP\n" ;} || DEFAULT_IP=$MGMT_IP
01-14
284
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
09-26
416
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交