当前位置:网站首页>Circular structure of shell system learning
Circular structure of shell system learning
2022-07-28 01:05:00 【edenliuJ】
With a list for Loop statement
class C Style for Loop statement
for Loop statement
With a list for Loop statement
The grammar is as follows :
for variable in {list}
do
statement1
...
doneIn the grammar above ,variable It is called a cyclic variable ,list It's a list , It can be a series of numbers or strings , The elements are separated by spaces ,do and done The structure between them is called circular body .
Here are a few examples :
#!/bin/bash
for var in 1 2 3 4 5 6
do
printf "$var"; printf " "
done
echo
for var in {1..6}
do
printf "$var"; printf " "
done
echo
for day in Mon Tue Wed Thu Fri Sat Sun
do
printf "$day"; printf " "
done
echo
for day in { Mon Tue Wed Thu Fri Sat Sun }
do
printf "$day"; printf " "
donegive the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-1.sh
1 2 3 4 5 6
1 2 3 4 5 6
Mon Tue Wed Thu Fri Sat Sun
{ Mon Tue Wed Thu Fri Sat Sun }
Note that strings cannot be used {} Curly brackets indicate a list .
In the above example for The steps of the cycle are 1, actually ,Shell Allow the user to set the step size , The grammar is as follows :
for variable in {start..end..step}
do
statement1
...
donestart Indicates the starting value ,end Indicates the end value ,step Indicating step size
Let's look at an example :
#!/bin/bash
sum=0
for i in {1..100..2}
do
((sum+=i))
done
echo "$sum"give the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-2.sh
2500
for The sentence is list It can also be Shell The result of the command output , such as ls The command will output a list
At present ls The command output of is as follows :
[email protected]:~/Documents/Shell/5$ ls
5-1.sh 5-2.sh 5-3.sh
Shell The code is as follows :
#!/bin/bash
for file in `ls`
do
echo "$file"
done
give the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-3.sh
5-1.sh
5-2.sh
5-3.sh
Also use wildcards * As a condition list , This * The function of number is to list all files in the current directory , The code is as follows :
#!/bin/bash
for file in *
do
echo "$file"
done
give the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-4.sh
5-1.sh
5-2.sh
5-3.sh
5-4.sh
Without a list for sentence
In some special cases ,for The condition list of the loop can be completely omitted , It is called without list for Loop statement . here ,Shell A list of conditions will be obtained from the command line , Take a look at the sample code
#!/bin/bash
for arg
do
echo "$arg"
doneWe output parameters a b c, give the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-5.sh a b c
a
b
c
And use $* The result of doing the condition parameter is consistent .
class C Style for Loop statement
stay Linux On ,C/C++ Is the most mainstream development language , In order to adapt to the habits of these users ,bash shell Classes are also provided C Style for loop , The grammar is as follows :
for ((expression1;expression2;expression3))
do
statement1
...
doneIllustrate with examples
#!/bin/bash
for ((i=1;i<5;++i))
do
echo "$i"
donegive the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-6.sh
1
2
3
4
Use for Loop through arrays
The grammar is as follows :
for var in ${array[*]}
do
statement1
...
doneamong array Represents the name of the array , See the example :
#!/bin/bash
alist=(Mon Tue Med)
for day in ${alist[*]}
do
echo "$day"
donegive the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-7.sh
Mon
Tue
Med
until Loop statement
until The syntax of the sentence is :
until expression
do
statement1
...
doneAmong them expression It's true return 0 when , Jump out of the loop .
while Loop statement
while The syntax of a sentence is related to until almost :
while expression
do
statement1
...
doneThe difference lies in , while The statement is when expression Conditions For false Return non 0 when , Jump out of the loop . And until Just the opposite
break and continue
Shell The statement provides break and continue Used to control the cycle .
break Just jump out of the loop ,continue Is to skip the current loop body continue Subsequent statements , Execute again from the beginning of the loop statement . Here you are C If you know the language experience, you won't talk much . add ,Shell Of break and continue Numerical parameters can be added later , The grammar is as follows :
break n
continue nbreak n To jump out of n Layer cycle ,continue n To skip n Layer cycle , Feel for example :
#!/bin/bash
for i in {1..6}
do
for j in {1..6}
do
if [ &j -eq 3 ]
then
break 2
fi
echo "$i $j"
done
donegive the result as follows :
[email protected]:~/Documents/Shell/5$ ./5-8.sh
1 1
1 2
Summary
This article mainly talks about Shell The cycle structure of , Include for,until,while, And control loop statements break and continue Usage of
边栏推荐
- Recommend a Hongmeng instant messaging software "fruit chat", which is a bit awesome!!
- Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
- 函数相关知识
- Examples of application of JMeter in performance testing
- 【C语言入门】ZZULIOJ 1026-1030
- Ddt+yaml implementation of data driven mechanism based on unittest
- 网络设备硬核技术内幕 防火墙与安全网关篇 (十)
- 数组相关知识
- Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
- [introduction to C language] zzulioj 1026-1030
猜你喜欢

How to smoothly go online after MySQL table splitting?

The program design questions of the 11th national competition of Bluebridge cup single chip microcomputer
![[CruiseControl]Build Result JSP](/img/80/11c2b539c217ecd6ba55668d3e71e9.png)
[CruiseControl]Build Result JSP
![[300 opencv routines] 241. Scale invariant feature transformation (SIFT)](/img/7a/a764c779c3162920c832325f89f340.png)
[300 opencv routines] 241. Scale invariant feature transformation (SIFT)

Rongyun IM & RTC capabilities on new sites

Analysis and recurrence of network security vulnerabilities

立即报名 | 云原生技术交流 Meetup 广州站已开启,8 月 6 号与你相遇!

Syntaxerror resolved: positive argument follows keyword argument

芯片行业常用英文术语最详细总结(图文快速掌握)

Recommend a Hongmeng instant messaging software "fruit chat", which is a bit awesome!!
随机推荐
Leetcode:1997. the first day after visiting all rooms [jump DP]
Jerry, if you turn on Bluetooth again, one for two. When the mobile phone is connected to the prototype, it will appear and cannot be connected [chapter]
Cross desktop web container evolution
canvas模拟输入框输入
Basic operations of MySQL database (I) --- Based on Database
Canvas analog input box input
函数相关知识
Swear, swear, swear
Ddt+yaml implementation of data driven mechanism based on unittest
Network device hard core technology insider firewall and security gateway (IX) virtualization artifact (II)
范德蒙德卷积 学习笔记
推荐系统-模型:wide&deep 模型
Count the six weapons of the domestic interface cooperation platform!
推荐系统-精排模型:xDeepFM
110. SAP UI5 FileUploader 控件深入介绍 - 为什么需要一个隐藏的 iframe
How to smoothly go online after MySQL table splitting?
Storage of deep planing data in memory
Network device hard core technology insider firewall and security gateway (VII) virtualization artifact (Part 1)
红队大杀器 Behinder_v4.0(冰蝎4.0)
福特SUV版“野马”正式下线,安全、舒适一个不落