当前位置:网站首页>Shell tutorial circular statements for, while, until usage
Shell tutorial circular statements for, while, until usage
2022-06-29 18:03:00 【Full stack programmer webmaster】
Loop statement :
Bash Shell There are three main ways of circulation in :for、while and until.
One 、for loop for The way circulation works , It's about serial elements , Put in the specified variable in order , Then repeat the enclosed command area ( stay do and done Between ), Until all the elements are exhausted .
among , Serialization is a combination of strings , Use each other $IFS The separator defined ( Such as the space character ) separate , These strings are called fields .
for The grammatical structure of is as follows :
for Variable in Serial
do
Carry out orders
done
explain :
That's ok 1, The fields in the serial are iterated into the variables
That's ok 2-4, This will then be repeated do and done The command area between , Until each field in the serial is processed .
flow chart
example 1:
use for The loop is created in the home directory aaa1-aaa10, And then in aaa1-aaa10 establish bbb1-bbb10 The catalog of
#!/bin/bash
for a in {
1..10}
do
mkdir /datas/aaa$a
cd /datas/aaa$a
for b in {
1..10}
do
mkdir bbb$b
done
done
#!/bin/bash
for k in $( seq 1 10 )
do
mkdir /home/kuangl/aaa${k}
cd /home/kuangl/aaa${k}
for l in $( seq 1 10 )
do
mkdir bbb${l}
cd /home/kuangl/aaa${k}
done
cd ..
done
explain :
That's ok 2,seq Used to generate all integers from one number to another .
That's ok 4, Create a folder in your home directory .
That's ok 6, Using a for Loop to create a folder
example 2
List var The size of the disk space occupied by the subdirectories under the directory .
#!/bin/bash
DIR="/var"
cd $DIR
for k in $(ls $DIR)
do
[ -d $k ] && du -sh $k
done
explain :
That's ok 4, Yes /var Every file in the directory , Conduct for Loop processing .
That's ok 6, If /var The file below is the directory , Then use du -sh Calculate the amount of disk space occupied by the directory .
Two 、while loop while Circular grammar :
1 while Conditions for testing
2 do
3 Carry out orders
4 done
explain :
That's ok 1, First, do the conditional test , If the return value is 0( The conditional test is true ), Then enter the cycle , Execute command area , otherwise
Don't go into the cycle , Introduce while command
That's ok 3, Execute command area , In these orders , There should be a command to change the conditional test , such , To have a chance to be in
Finish execution after limited steps while loop ( Unless you want to execute an infinite loop ).
That's ok 4, Back to the line 1, perform while command
flow chart :
example 1
while loop , The classic usage is with turn input , Read the contents of the file , Doing this :
#!/bin/bash
while read a
do
echo $a
done < /datas/6files
1 #!/bin/bash
2 while read kuangl
3 do
4 echo ${kuangl}
5 done < /home/kuangl/scripts/testfile
explain :
That's ok 2, Use read There are standard inputs to read data , Put variables kuangl in , If the data read is not empty , Go into the cycle .
That's ok 4, Show the data of changing profession
That's ok 5, take /home/kuangl/scripts/testfile The content steering input will give read Read .
example 2
#!/bin/bash
declare -i i=1
declare -i sum=0
while ((i<=10))
do
let sum+=i
let i++
done
echo $sum
#!/bin/bash
declare -i i=1
declare -i sum=0
while ((i<=10))
do
let sum+=i
let ++i
done
echo $sum
explain :
That's ok 2-3, Statement i and sum It's an integer
That's ok 4, If the condition test : as long as i The value is less than or equal to 10, Just execute the loop .
That's ok 6,sum+=i and sum=sum+i It's the same ,sum Add up i.
That's ok 7,i Increasing the value of the 1, This line is the command to change the conditional test , once i Greater than 10, Can terminate the cycle .
That's ok 8, encounter done, Back to the line 6 To perform conditional tests
That's ok 9, Show sum The value of is 55
example 3 while99 Multiplication table
#!/bin/bash
a=1
b=1
while ((a <=9))
do
while ((b<=a))
do
let "c=a*b" # Declare variables c
echo -n "$a*$b=$c "
let b++
done
let a++
let b=1 # Because every multiplication table is 1 Start to multiply , therefore b To reset
echo "" # Display to screen wrap
done
explain :
That's ok 8, Declare variables c
That's ok 9,echo The format of the output display ,-n Don't wrap output
That's ok 13,let b=1 Because every multiplication table is 1 Start to multiply , therefore b To reset
3、 ... and 、until loop while The conditional test of the loop is the true value ,until A loop is a false value .
until Circular grammar :
1 until Conditions for testing
2 do
3 Carry out orders
4 done
explain : That's ok 1, If the conditional test result is false ( The return value is not 0), Go into the cycle .
That's ok 3, Execute command area . In these orders , There should be a command to change the conditional test , It looks like , Only then has the opportunity to finish the execution after the limited steps until loop ( Unless you want to execute an infinite loop ).
That's ok 4, Back to the line 1, perform until command .
flow chart :
example 1
1 #!/bin/bash
2 declare -i i=10
3 declare -i sum=0
4 until ((i>10))
5 do
6 let sum+=i
7 let ++i
8 done
9 echo $sum
explain : That's ok 2-3, Statement i and sum It's an integer
That's ok 4, If the condition test : as long as i The value does not exceed 10, Go into the cycle .
That's ok 6,sum+=i and sum=sum+i It's the same ,sum Add up i.
That's ok 7,i Increasing the value of the 1, This line is the command to change the conditional test , once i Greater than 10, Can terminate the cycle .
That's ok 8, encounter done, Back to the line 6 To perform conditional tests
That's ok 9, Show sum The value of is 10
example 2 until99 Multiplication table
#!/bin/bash
a=1
b=1
until ((a>9)) #until and while contrary , Conditional execution , The conditions are really done end
do
until ((b>a))
do
let "c=a*b"
echo -n "$a*$b=$c "
let b++
done
let a++
let b=1
echo ""
done
explain :
That's ok 4, If the condition test : as long as a The value does not exceed 9, Go into the cycle , Once you surpass 9 No execution ,until and while Opposite conditions , The conditions are really done end
That's ok 6,b>a, once b Greater than a I'm not going to implement it
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/101817.html Link to the original text :https://javaforall.cn
边栏推荐
- 工作流模块Jar包启动报错:liquibase – Waiting for changelog lock….
- Bloom filter:
- 3H proficient in opencv (VIII) - shape detection
- 字典树(随学)
- Precondition end of script headers or end of script output before headers
- Find the maximum XOR value in the sequence given a number (01 Dictionary)
- 面试中问最常问的海量数据处理你拿捏了没?
- SSH协议学习笔记
- 3h精通OpenCV(六)-图像堆叠
- SRM supplier collaborative management system function introduction
猜你喜欢
2022 spring summer collection koreano essential reshapes the vitality of fashion
Yurun multidimensional makes efforts in the charity field and bravely resists the corporate public welfare banner
小程序容器是什么技术?能助力物联网企业红海突围?
Prevent form resubmission based on annotations and interceptors
YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署
ISO 32000-2 国际标准7.7
Niuke Xiaobai monthly race 52 E group logarithmic sum (inclusion exclusion theorem + dichotomy)
Let Google search your blog
ABC253 D FizzBuzz Sum Hard(容斥定理)
软件测试——基础理论知识你都不一定看得懂
随机推荐
Spingmvc requests and responses
How QQ opens online customer service
Web Scraping with Beautiful Soup for Data Scientist
Industry application of smart city based on GIS 3D visualization
布隆过滤器:
国内酒店交易DDD应用与实践——理论篇
Niuke small Bai monthly race 52 D ring insectivorous (feet +st table)
Dictionary tree (optional)
让 Google 搜索到自己的博客
【TcaplusDB知识库】TcaplusDB单据受理-建表审批介绍
Issue 42: is it necessary for MySQL to have multiple column partitions
YoloV6+TensorRT+ONNX:基于WIN10+TensorRT8+YoloV6+ONNX的部署
Set double click to run the jar file
[try to hack] cookies and sessions
Software testing - you may not understand the basic theoretical knowledge
3h精通OpenCV(五)-透视变换
Digital twin energy system, creating a "perspective" in the low-carbon era
MaxCompute Studio
Repair of JSON parsing errors in a collection
牛客小Bai月赛52 D 环上食虫(尺取+st表)