当前位置:网站首页>For loop of shell script
For loop of shell script
2022-07-21 04:20:00 【Oreo WM】
shell The script for loop
Circulatory :for、while、until
What is? for Circulation? ?
for Circular logic : It is to assign the elements in the circular list to variables , Each assignment will execute a loop ,done It marks the end of a cycle . Until the elements in the list are exhausted , End the cycle ( You can understand that , Because of something , You make your partner angry , Do you have to apologize , It's no use apologizing once , Is it a second time , It's a cycle Until your partner forgives you This cycle ends . You can understand that )
Format :
for Variable in value 1 value 2 value 3
do It is equivalent to the upper part of the brace
echo Statement program
done Equivalent to the lower part of the brace
Bring each value into your variable and enter one by one Then output the waiting set
!
Application example :1
The first format $* Treat all parameters as a whole
Second format [email protected] Indicates that each parameter is partitioned
stay It can be seen when the quantity is large ∗ and * and ∗ and @ What's the difference
Case study :
$* The output is a whole
vim textfor.sh
Comment on the environment #!/bin/bash
for i in " ∗ ∗ " this individual yes What Well It means thinking : yes hold **" What does this mean? : It's a ∗∗" this individual yes What Well It means thinking : yes hold * * Assign to i This variable hold &* Give this value to i Then output him echo Things that are If it's one time Just output once If it's separate That's it Multiple corresponding outputs
do
echo “the num is $i”
done
chmod 744 perhaps +x textfor.sh Give an executive authority
./textfor.sh perform
[email protected] Separate output
vim textfor.sh
Comment on the environment #!/bin/bash
for i in “[email protected]”
do
echo"the num is $i"
done
chmod 744 perhaps +x textfor.sh Give an executive authority
./textfor.sh 10 20 30 perform
Handled separately Pay attention to ∗ and * and ∗ and @ What's the difference
$* Overall treatment
[email protected] Separate the
Like hold 10 20 30 I don't know the number of the sum of the values of To use [email protected] It's convenient
The second basic grammar
for( Initial value ; Cycle control conditions ; Variable change )
do
Program
done
Case study from 1 Add to 100 The value output of 1 All the way up to 100
vim testfor2.sh Create a for Script for
!/bin/bash
sum=0 When sum It means and
for((i=1;i<=100;i++)) What does it mean in parentheses i be equal to 1 ;i<=100 It means when i Greater than or equal to 100 when ;i++ It means i++ It's using i Then add another
do
SUM= [ [ [SUM+$i] In parentheses is a cumulative In brackets sum and i After accumulation, give it to the front sum Then output the final result Note that brackets are used here 【】
done
echo “SUM=$SUM” Output the final result
~
chmod 744 perhaps +x textfor.sh Give an executive authority
./testfor2.sh
2.3 Multiplication table
utilize for loop , Print 99 Multiplication table
#!/bin/bash
for ((i=1;i<=9;i++)) # You can also write here for i in {1…9}
do
for ((j=1;j<=i;j++)) # You can also write here for j in seq $i
do echo -n -e “ j ∗ j* j∗i=$[j*i]\t” #-n Don't wrap output ;-e Use \ Escape character ;\t Horizontal tabulation ; If not -e, be \t Is a normal character
done
echo
done
3、 ... and . summary
1.for Loops are widely used in actual generation , Avoid manual repetition to operate something
2.for The list in the loop supports regular expressions
3.for Use in loop (()), Support in double brackets C The command of language
4.for Variables in the loop , Just cycle to get values from the list , Then proceed do operation , So the number of parameters in the value list , Determines the number of cycles
边栏推荐
- pinctrl
- Please tell me about flink1.15 flinksql Kafka topic. If you don't create a submission job in advance, it will fail
- The difference between rpm and yum
- Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part IV)
- Uni app - plugin [app cloud packaging] installation failed! (the app cloud packaging plug-in installation fails when the app is packaged) solution
- 进程和计划任务管理
- BeanUtils.copyProperties()和JSONObject.parseObject()是什么拷贝类型(浅拷贝或者深拷贝)
- UGUI——LayoutGroup
- UGUI——EventSystems和射线检测
- 栈模拟队列
猜你喜欢
WordPress 6.0.1 新版已经发布,建议全部更新。
正则表达式和shell三剑客(grep、egrep、sed和awk)工具
JSON解析
WordPress opens sitemap XML, a piece of code to solve.
Rust安装并实现一个helloworld
Given a positive integer n, it is expressed as the addition of numbers 1,3,7,15. Please code to find the combination that minimizes the total number of occurrences of the above numbers (each number ca
McCabe度量方法计算程序复杂度
Shell的各种变量和变量运算
UGUI——EventSystems和射线检测
Don't know how to learn MySQL? It's enough to finish the 50 questions of Niuke! (Part VI)
随机推荐
请教一下,如果我同步的是mysql binlog的数据 并且没有加特定的日志,我怎么查看当前程序同步
请问一下 flink1.15 flinksql kafka topic如果不提前创建提交作业就会失败
豆瓣评分9.4!跟邱锡鹏教授一起学「神经网络与深度学习」,7月19日开课
Uni app - plugin [app cloud packaging] installation failed! (the app cloud packaging plug-in installation fails when the app is packaged) solution
UGUI——LayoutGroup
Web framework foreplay
Chinese NLP toolkits Chinese NLP tools
"Dry goods experiment" Huawei DHCP + single arm routing experiment
UGUI官方优化文档翻译
JSON parsing
解读最新ECCV 2022工作:组合式扩散模型
Random number (random header file)
cocos2dx 引擎中的一些技巧
Unity实现人物移动和镜头跟随
Compose实现webView文件选择
Pytorch realizes handwritten digit recognition | MNIST data set (fully connected neural network)
Ugui - text and texteffect
UGUI——Graphic
WordPress开启sitemap.xml,一段代码解决。
Combined with the source code, see "cocos2dx-3.0 as I understand" - particle system