当前位置:网站首页>Shell基本语法--流程控制
Shell基本语法--流程控制
2022-06-29 17:59:00 【__万波__】
if else 语句
基本结构
注意:
if和elif语句后面要跟then才能开始执行语句块then可以换行写, 写在同一行要加分号隔开, 建议写在同一行, 结构会更清晰- 没有 else if 这种语句, 只能写
elif
if [ ]; then
# if 语句块
elif [ ]; then
# 语句块
else
# 语句块
fi
条件运算符
[]与[[]]的不同之处
[[]]运算符只有bash中才有, 其他shell如ash, dash中是没有这个语法的[[]]有[]中没有的功能, 比如比较两个字符串是否相等, 且字符串中有空格的时候, 使用[]时必须加双引号而使用if [ "$str" = "hello man" ]; then[[]]时就不必担心这个问题if [[ $str = "hello man" ]]; then- 逻辑运算. 使用
[]条件运算符时,&&,||,<,>这几个符号都会被当作命令的一部分, 而不是当作逻辑运算符, 使用[[]]条件运算符时就不会有这个问题, 如:# 下面的 > 符号会被当作重定向命令, && 也作为命令的一部分, 会创建200 和 100两个文件, 且语句会报错: [: missing `]' if [ 300 > 200 && 200 > 100 ]; then # 下面的语句就可以正常的执行逻辑运算 if [[ 300 > 200 && 200 > 100 ]]; then- 模糊匹配. 使用
[[]]时可以使用=~运算符及正则语法实现模糊匹配如匹配yes:# 使用 [] 时只能使用如下写法 if [ $str = "y" -o $str = "yes" ];then # 使用 [[]] 时就可以有如下写法 if [[ $str =~ ^y(es)$ ]];then echo ${ BASH_REMATCH[1]} # 匹配到的值存在这个系统变量中 ... # 还可以使用下面的通配符写法 if [[ $str = y* ]];then- 经验, 一般做判断时会使用
[]条件运算符, 但在做字符串比较时, 最好用[[]]会降低出错概率
条件标识符
判断数字
if [ $a -eq $b ]; then # 等于 equal
if [ $a -ne $b ]; then # 不等于 not equal
if [ $a -gt $b ]; then # 大于 greater than
if [ $a -ge $b ]; then # 大于等于 greater equal
if [ $a -lt $b ]; then # 小于 little than
if [ $a -le $b ]; then # 小于等于 little equal
判断字符串
# 使用 [] 运算符
if [ -z "$str" ]; then # 判断为空(字符串变量加引号)
if [ -n "$str" ]; then # 判断不为空(字符串变量加引号)
if [ "$str" = "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" == "abc" ]; then # 判断相等(字符串变量加引号)
if [ "$str" != "abc" ]; then # 判断不相等(字符串变量加引号)
if [ "$str1" \> "$str2" ]; then # 判断大于(字符串变量加引号)
if [ "$str1" \< "$str2" ]; then # 判断小于
# 使用 [[]] 运算符
if [[ -z $str ]]; then # 判断为空
if [[ -n $str ]]; then # 判断不为空
if [[ $str = "ab c" ]]; then # 判断相等
if [[ $str == "ab c" ]]; then # 判断相等
if [[ $str != "ab c" ]]; then # 判断不相等
if [[ $str1 > $str2 ]]; then # 判断大于
if [[ $str1 < $str2 ]]; then # 判断小于
文件判断
if [ -e $file ]; then # 判断文件存在
if [ -L $file ]; then # 判断文件存在且是链接文件(要最先判断, 否则会先去判断软链接指向的文件的其他属性)
if [ -s $file ]; then # 判断文件存在且是字符设备
if [ -b $file ]; then # 判断文件存在且是块设备文件
if [ -d $file ]; then # 判断文件存在且是文件夹/目录
if [ -f $file ]; then # 判断文件存在且是普通文件
if [ -S $file ]; then # 判断文件存在且是套接字文件
if [ -p $file ]; then # 判断文件存在且是管道文件
# 对文件权限的判断
if [ -r $file ]; then # 判断文件具有可读权限
if [ -w $file ]; then # 判断文件具有可写权限
if [ -x $file ]; then # 判断文件具有可执行权限
逻辑运算
# 使用 [] 时
if [ $a -o $b ];then # 或 or
if [ $a -a $b ];then # 且 and
if [ ! $a ];then # 非
if [ expression1 ] || [ expression2 ];then # 或
if [ expression1 ] && [ expression2 ];then # 且
if ! [ expression ] # 非
# 使用 [[]]时
if [[ $a && $b ]];then # 或
if [[ $a && $b ]];then # 且
if [[ ! $a ]];then # 非
if [[ expression1 ]] || [[ expression2 ]];then # 或
if [[ expression1 ]] && [[ expression2 ]];then # 且
if ! [[ expression ]] # 非
switch case 选择
case ${var} in
[1-9]) # 范围
# do something
;;
[^a-zA-Z]) # 非此范围
# do something
;;
*)
#do something
;;
esac
select in 选择
select var in ch_a ch_b ch_c ch_d
do
#经常结合case in 语句使用
done
while 循环
while test
do
# 循环体
done
while true # 死循环的写法
do
done
for 循环
for ((i=0; i<=100; i++))
do
# 循环体
done
for var # 不加任何参数会循环所有的位置参数
do
done
for in 循环
for var in a b c d e
do
#
done
for var in {
1..100}
do
#
done
break 与 continue
shell 中的break和continue与普通C系列程序中的同名关键词有着不同的行为
break n: 退出n层循环continue n: 跳过n层循环, 执行
边栏推荐
- 工作流模块Jar包启动报错:liquibase – Waiting for changelog lock….
- 自动化软件测试 - 利用短信转发器结合Selenium读取短信验证码
- 行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
- Shell tutorial circular statements for, while, until usage
- Programmer Resource Recommendation Guide
- MaxCompute Studio
- Goldfish rhca memoirs: do447 build advanced job workflow -- create job template survey to set work variables
- Adobe Premiere基础-不透明度(蒙版)(十一)
- Lodash deep copy usage
- markdown知识轻轻来袭
猜你喜欢

Distributed | several steps of rapid read / write separation

让 Google 搜索到自己的博客

软件测试——基础理论知识你都不一定看得懂

MySQL -connector/j driver download

Spingmvc requests and responses

【网络是怎么连接的】第三章 探索集线器,交换机和路由器

jdbc_相关代码

My first experience of remote office | community essay solicitation

Serial port experiment based on stm32f103zet6 library function

Yolov6+tensorrt+onnx: deployment based on win10+tensorrt8+yolov6+onnx
随机推荐
3H proficient in opencv (VI) - image stacking
Adobe Premiere基础-不透明度(蒙版)(十一)
工作流模块Jar包启动报错:liquibase – Waiting for changelog lock….
Have you grasped the most frequently asked question in the interview about massive data processing?
Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)
Adobe Premiere Foundation - réglage du son (correction du volume, réduction du bruit, tonalité téléphonique, changement de hauteur, égaliseur de paramètres) (XVIII)
The soft youth under the blessing of devcloud makes education "smart" in the cloud
行程卡“摘星”热搜第一!刺激旅游产品搜索量齐上涨
QQ如何开通在线客服
等保测评结论为差,是不是表示等保工作白做了?
[tcapulusdb knowledge base] tcapulusdb system user group introduction
Longest XOR path (dfs+01trie)
让 Google 搜索到自己的博客
idea怎么使用?
Automatic software test - read SMS verification code using SMS transponder and selenium
Opencv+yolo-v3 for target tracking
MySQL -connector/j driver download
Xiaomai technology x hologres: high availability of real-time data warehouse construction of ten billion level advertising
Prevent form resubmission based on annotations and interceptors
[网鼎杯 2020 青龙组]AreUSerialz