当前位置:网站首页>Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
Shell流程控制(重点)、if 判断、case 语句、let用法、for 循环中有for (( 初始值;循环控制条件;变量变化 ))和for 变量 in 值 1 值 2 值 3… 、while 循环
2022-07-27 09:49:00 【Redamancy06】
1.流程控制(重点)
1.1if 判断
1.1.1基本语法
注意事项:
①[ 条件判断式 ],中括号和条件判断式之间必须有空格
②if 后要有空格
1.1.1.1单分支
if [ 条件判断式 ];then
程序
fi
或者
if [ 条件判断式 ]
then
程序
fi
1.1.1.2多分支
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
else
程序
fi
1.1.2案例
1.1.2.1单分支



当这样写的时候,没有传入参数时,会报错的,所以要按照下面的做法做


这样的话就不会报错了,因为要是不输入参数的时候两边也都不会为空
1.1.2.2用逻辑与&&逻辑或||连接(单分支)
1.1.2.2.1逻辑与&&

在中括号里面的话就不用&&而是用-a(-and)

1.1.2.2.2逻辑或||

在中括号里面的话就不用||而是用-o(-or)
1.1.2.3多分支

…


1.2case 语句
1.2.1基本语法
case $变量名 in
"值 1")
如果变量的值等于值 1,则执行程序 1
;;
"值 2")
如果变量的值等于值 2,则执行程序 2
;;
…省略其他分支…
*)
如果变量的值都不是以上的值,则执行此程序
;;
esac
1.2.2注意事项:
(1)case 行尾必须为单词“in”,每一个模式匹配必须以右括号“)”结束。 (2)双分号“;;”表示命令序列结束,相当于 java 中的 break。
(3)最后的“*)”表示默认模式,相当于 java 中的 default。
1.2.3案例cast_test.sh



1.3for 循环
1.3.1基本语法1
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
1.3.2基本语法 2
for 变量 in 值 1 值 2 值 3…
do
程序
done
1.3.3案例
1.3.3.1for (( 初始值;循环控制条件;变量变化 ))



1.3.3.2“{}”在linux shell里面表示生成序列

{1…100}表示从1到100,中间是两个点,别看错了
1.3.3.3for 变量 in 值 1 值 2 值 3…

1.3.3.2在for循环中$*和[email protected]的区别

当$*和[email protected]没有加双引号的时候输出结果为下:

他们两个的输出结果一致

当加上双引号后。结果如下:
当它们被双引号“”包含时,
$*会将所有的参数作为一个整体,以“$1 $2 …$n”的形式输 出所有参数;
[email protected]会将各个参数分开,以“$1” “$2”…“$n”的形式输出所有参数。
1.4while 循环
1.4.1基本语法
while [ 条件判断式 ]
do
程序
done
1.4.2从1加到100



1.4.3let用法
在新版shell中支持用let的,他的语言是和高级语言差不多的


边栏推荐
- I haven't delivered books for a long time, and I feel uncomfortable all over
- 吃透Chisel语言.26.Chisel进阶之输入信号处理(二)——多数表决器滤波、函数抽象和异步复位
- 聊聊索引失效的10种场景,太坑了
- July training (day 13) - two way linked list
- Esp8266 Arduino programming example - interrupt
- A ride into Qinchuan -- a brief talk on how beego Autorouter works
- Brush the title "sword finger offer" day03
- 达梦 PARTGROUPDEF是自定义的对象吗?
- How to install cpolar intranet penetration on raspberry pie
- System parameter constant table of system architecture:
猜你喜欢

Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children

食品安全 | 无糖是真的没有糖吗?这些真相要知道

华为交换机双上行组网Smart-link配置指南

NFT系统开发-教程

食品安全 | 还在吃酵米面吗?当心这些食物有毒!

Qt 学习(二) —— Qt Creator简单介绍

Summary of binary tree exercises

Interview Essentials: shrimp skin server 15 consecutive questions

并发之线程状态转换

I haven't delivered books for a long time, and I feel uncomfortable all over
随机推荐
C # set different text watermarks for each page of word
吃透Chisel语言.26.Chisel进阶之输入信号处理(二)——多数表决器滤波、函数抽象和异步复位
Understand chisel language. 22. Chisel sequential circuit (II) -- detailed explanation of chisel counter: counter, timer and pulse width modulation
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
Why do microservices have to have API gateways?
At the end of the year, I'll teach you how to get high performance!
Meeting seating function of conference OA project & Implementation of meeting submission for approval
WGAN、WGAN-GP、BigGAN
吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
面试必备:虾皮服务端15连问
Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
In depth analysis, sub database and sub table are the most powerful auxiliary sharding sphere
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]
Understand chisel language. 27. Chisel advanced finite state machine (I) -- basic finite state machine (Moore machine)
拜托!面试请不要再问我 Ribbon 的架构原理
3D修复论文:Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks
Expose a technology boss from a poor family
食品安全 | 还在吃酵米面吗?当心这些食物有毒!
Leetcode.814. binary tree pruning____ DFS
After one year, the paper was finally accepted by the international summit