当前位置:网站首页>Shell script -for loop and for int loop
Shell script -for loop and for int loop
2022-07-01 08:47:00 【Little snail's way】
for loop
for((exp1; exp2; exp3))
do
statements
done
Some notes :
- exp1、exp2、exp3 Are three expressions , among exp2 It's the judgment condition ,for The loop is based on exp2 To decide whether to continue the next cycle ;
- statements It's a loop body statement , There can be one , There can be more than one ;
doanddoneyes Shell Keywords in .
Code : Calculate from 1 Add to 100 And .
#!/bin/bash
sum=0
for ((i=1; i<=100; i++))
do
((sum += i))
done
echo "The sum is: $sum"
Output :
The sum is: 5050
for in loop
for variable in value_list
do
statements
done
variable Said variable ,value_list Indicates the value list ,in yes Shell Keywords in .
Each cycle starts with value_list To assign a value to a variable variable, And then into the circulatory body (do and done Part between ), Execute in loop body statements. Until the end of taking value_list All the values in , The cycle is over .
Code 1
#!/bin/bash
sum=0
for n in 1 2 3 4 5 6
do
echo $n
((sum+=n))
done
echo "The sum is "$sum
Output :
1
2
3
4
5
6
The sum is 21
Yes value_list Explanation
1) Give specific values directly
Can be in in The specific value is directly given after the keyword , Multiple values are separated by spaces , such as 1 2 3 4 5、“abc” “390” "tom" etc. .
Code :
#!/bin/bash
for str in "aaa" "bbb" "ccc" "ddd"
do
echo $str
done
Output :
aaa
bbb
ccc
ddd
2) Give a range of values
The specific format of a value range is :
{start..end}
start Indicates the starting value ,end Indicates the end value ; Note that the middle is connected by two dots , Instead of three dots . According to the author's actual measurement , This form only supports numbers and letters .
Code 1: Calculate from 1 Add to 100 And
#!/bin/bash
sum=0
for n in {1..100}
do
((sum+=n))
done
echo $sum
Output :
5050
Code 2: Output from A To z All characters between :
#!/bin/bash
for c in {A..z}
do
printf "%c" $c
done
Output :
ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz
You can find ,Shell It's based on ASCII Code table to output .
3) Use the execution result of the command
Code 1: Calculate from 1 To 100 Between all even numbers and :
#!/bin/bash
sum=0
for n in $(seq 2 2 100)
do
((sum+=n))
done
echo $sum
Output :
2550
seq It's a Linux command , Used to generate integers in a certain range , And you can set the step size .seq 2 2 100 From 2 Start , Each time 2, To 100 end .
Code 2: List all under the current directory Shell Script files :
#!/bin/bash
for filename in $(ls *.sh)
do
echo $filename
done
Output :
demo.sh
test.sh
abc.sh
4) Use Shell wildcard
Shell Wildcards can be thought of as a kind of refined regular expression , Usually used to match directories or files , Not the text
Code : Display all script files in the current directory
#!/bin/bash
for filename in *.sh
do
echo $filename
done
Output :
demo.sh
test.sh
abc.sh
边栏推荐
猜你喜欢
随机推荐
3、Modbus通讯协议详解
Interrupt sharing variables with other functions and protection of critical resources
【MFC开发(16)】树形控件Tree Control
NFT监管要点和海外政策
Nacos - 配置管理
In depth learning training sample amplification and tag name modification
Shell脚本-变量的定义、赋值和删除
Shell脚本-数组定义以及获取数组元素
Personal decoration notes
The meaning of yolov5 training visualization index
爬虫知识点总结
《单片机原理及应用》-片外拓展
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
Model and view of QT
Shell脚本-case in语句
挖财打新股安全吗
Foundation: 2 The essence of image
The era of low threshold programmers is gone forever behind the sharp increase in the number of school recruitment for Internet companies
Shell脚本-echo命令 转义符
中考体育项目满分标准(深圳、安徽、湖北)









