Shell 三目运算(详细案例)

简介: Shell 三目运算(详细案例)
  • 在其他语法中常见的三目运算方式:
表达式 ? 表达式 : 表达式 ;
  • 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
相关文章
|
4月前
|
Shell Linux C++
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
Linux C/C++ 开发(学习笔记二):Shell脚本编程案例
86 0
|
10月前
|
Shell 开发工具 C语言
2.shell脚本基本操作及案例
2.shell脚本基本操作及案例
72 0
|
10月前
|
分布式计算 Hadoop Java
17 案例:开发shell采集脚本
17 案例:开发shell采集脚本
68 0
|
监控 关系型数据库 MySQL
Shell脚本案例大全
Shell脚本案例大全
80 1
|
4月前
|
Shell Linux Perl
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门(第二天学习)
Shell基础学习---3、Read读取控制台输入、函数、综合应用案例:归档文件、正则表达式入门
|
4月前
|
Shell Linux 测试技术
一个案例学习bat和shell脚本的编写
一个案例学习bat和shell脚本的编写
|
12月前
|
Shell
Shell 三目运算(详细案例)
Shell 三目运算(详细案例)
140 1
|
Shell Perl
Shell基础学习---4、文本处理工具、综合应用案例(归档文件、发送信息)
Shell基础学习---4、文本处理工具、综合应用案例(归档文件、发送信息)
|
Shell
Shell case 语法简单案例
Shell case 语法简单案例
56 0
|
10天前
|
Ubuntu Linux Shell
在Linux中,如何使用shell脚本判断某个服务是否正在运行?
在Linux中,如何使用shell脚本判断某个服务是否正在运行?