当前位置:网站首页>Shell脚本-for循环和for int循环
Shell脚本-for循环和for int循环
2022-07-01 08:36:00 【小蜗牛的路】
for 循环
for((exp1; exp2; exp3))
do
statements
done
几点说明:
- exp1、exp2、exp3 是三个表达式,其中 exp2 是判断条件,for 循环根据 exp2 的结果来决定是否继续下一次循环;
- statements 是循环体语句,可以有一条,也可以有多条;
do和done是 Shell 中的关键字。
代码:计算从 1 加到 100 的和。
#!/bin/bash
sum=0
for ((i=1; i<=100; i++))
do
((sum += i))
done
echo "The sum is: $sum"
输出:
The sum is: 5050
for in 循环
for variable in value_list
do
statements
done
variable 表示变量,value_list 表示取值列表,in 是 Shell 中的关键字。
每次循环都会从 value_list 中取出一个值赋给变量 variable,然后进入循环体(do 和 done 之间的部分),执行循环体中的 statements。直到取完 value_list 中的所有值,循环就结束了。
代码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
输出:
1
2
3
4
5
6
The sum is 21
对 value_list 的说明
1) 直接给出具体的值
可以在 in 关键字后面直接给出具体的值,多个值之间以空格分隔,比如1 2 3 4 5、“abc” “390” "tom"等。
代码:
#!/bin/bash
for str in "aaa" "bbb" "ccc" "ddd"
do
echo $str
done
输出:
aaa
bbb
ccc
ddd
2) 给出一个取值范围
给出一个取值范围的具体格式为:
{start..end}
start 表示起始值,end 表示终止值;注意中间用两个点号相连,而不是三个点号。根据笔者的实测,这种形式只支持数字和字母。
代码1:计算从 1 加到 100 的和
#!/bin/bash
sum=0
for n in {1..100}
do
((sum+=n))
done
echo $sum
输出:
5050
代码2:输出从 A 到 z 之间的所有字符:
#!/bin/bash
for c in {A..z}
do
printf "%c" $c
done
输出:
ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz
可以发现,Shell 是根据 ASCII 码表来输出的。
3) 使用命令的执行结果
代码1:计算从 1 到 100 之间所有偶数的和:
#!/bin/bash
sum=0
for n in $(seq 2 2 100)
do
((sum+=n))
done
echo $sum
输出:
2550
seq 是一个 Linux 命令,用来产生某个范围内的整数,并且可以设置步长。seq 2 2 100表示从 2 开始,每次增加 2,到 100 结束。
代码2:列出当前目录下的所有 Shell 脚本文件:
#!/bin/bash
for filename in $(ls *.sh)
do
echo $filename
done
输出:
demo.sh
test.sh
abc.sh
4) 使用 Shell 通配符
Shell 通配符可以认为是一种精简化的正则表达式,通常用来匹配目录或者文件,而不是文本
代码:显示当前目录下的所有脚本文件
#!/bin/bash
for filename in *.sh
do
echo $filename
done
输出:
demo.sh
test.sh
abc.sh
边栏推荐
- NIO-零拷贝
- TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
- SPL installation and basic use (II)
- View drawing process analysis
- 5mo3 UHI HII HII 17mn4 19Mn6 executive standard
- Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
- SPL Introduction (I)
- Only in China! Alicloud container service enters the Forrester leader quadrant
- 基础:2.图像的本质
- 2022.2.15
猜你喜欢

There are many problems in sewage treatment, and the automatic control system of pump station is solved in this way

Matlab tips (16) consistency verification of matrix eigenvector eigenvalue solution -- analytic hierarchy process

3、Modbus通讯协议详解

"Analysis of 43 cases of MATLAB neural network": Chapter 30 design of combined classifier based on random forest idea - breast cancer diagnosis

Maneuvering target tracking -- current statistical model (CS model) extended Kalman filter / unscented Kalman filter matlab implementation

2022.2.15

Audio-AudioRecord create(一)

Matlab tips (23) matrix analysis -- simulated annealing

3. Detailed explanation of Modbus communication protocol

基础:2.图像的本质
随机推荐
Intelligent water supply system solution
R语言观察日志(part24)--初始化设置
为什么LTD独立站就是Web3.0网站!
win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
爬虫知识点总结
NFT监管要点和海外政策
固定资产管理系统让企业动态掌握资产情况
Suivi des cibles de manoeuvre - - mise en oeuvre du modèle statistique actuel (modèle CS) filtre Kalman étendu / filtre Kalman sans trace par MATLAB
C语言学生信息管理系统
基于Gazebo的无人机管道检测
Intelligent constant pressure irrigation system
2022 examination summary of quality controller civil engineering direction post skills (quality controller) and reexamination examination of quality controller civil engineering direction post skills
Memory size end
3. Detailed explanation of Modbus communication protocol
MATLAB【函数和图像】
一文纵览主流 NFT 市场平台版税、服务费设计
《单片机原理及应用》—定时器、串行通信和中断系统
C basic knowledge review (Part 4 of 4)
中小企业固定资产管理办法哪种好?
【js逆向】md5加密参数破解