第三章、Linux if case判断语句
2022/7/7 5:21:27
本文主要是介绍第三章、Linux if case判断语句,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
if 语句
if then fi
单层,单分支
if condition then sentence1 sentence2 ... fi
eg:
#! /bin/bash a=3 b=4 if [ "${a}" -le "${b}" ] && [ "${a}" -ne 2 ] then echo "${a} is in the range" fi
其他格式
if condition; then sentence1 sentence2 ... fi
#! /bin/bash a=3 b=4 if [ "${a}" -le "${b}" ] && [ "${a}" -ne 2 ];then echo "${a} is in the range";fi
if then else fi
if condition then sentence1 sentence2 ... else sentence1 sentence2 fi
他也是有其他形式的缩写的,记得有些地方需要加上分号。
#! /bin/bash filename='expr.sh' if [ -e "${filename}" ]; then echo "${filename} exist"; else echo "${filename} not exist"; fi
if then elif then elif then fi
if condition then sentence1 sentence2 ... elif condition then sentence1 sentence2 elif condition then sentence1 sentence2 else # 这个 else 可以不要 sentence1 sentence2 fi
#! /bin/bash a=4 if [ "${a}" -eq 1 ] then echo "${a}=1" elif [ "${a}" -eq 2 ] then echo "${a}=2" elif [ "${a}" -eq 3 ] then echo "${a}=3" else echo "${a} < 1 || ${a} > 3" fi
case 语句
case "${变量名称}" in value1) sentence1 sentence2 ... ;; # 类似于 C/C++中的 break value2) sentence1 sentence2 ... ;; # 类似于 C/C++中的 break *) # 类似于 C/C++ switch中的 default, * 是模式字符串 sentence1 sentence2 ... ;; # 类似于 C/C++中的 break esac
上文的value
是模式字符串,可以使用通配符,如果一个模式字符串中包含多个模式,需要用|
分隔开
实例:
#! /bin/bash choice=$1 echo "choice = ${choice}" case "${choice}" in "-a"|"-b") echo "${choice} is -a or -b";; -[cs]) echo "${choice} is -c or -s";; *) echo "others" esac
参考文献
欢迎报名Y总的Linux基础课
Linux 教程(第四版) - 孟庆昌
这篇关于第三章、Linux if case判断语句的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-09-11Linux部署Scrapy学习:入门级指南
- 2024-09-11Linux部署Scrapy:入门级指南
- 2024-08-21【Linux】分区向左扩容的方法
- 2024-08-21【Linux】gnome桌面环境切换KDE Plasma
- 2024-08-19如何安装 VMware Tools (macOS, Linux, Windows)
- 2024-08-15Linux部署Scrapy教程:入门级指南
- 2024-07-29linux命令行下好用的性能监控工具atop
- 2024-07-04Linux系统上离线升级SSH服务的具体操作步骤-icode9专业技术文章分享
- 2024-06-0600-macOS和Linux安装和管理多个Python版本
- 2024-03-30[译]漫画SELinux概念