当前位置:网站首页>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
边栏推荐
- 深度学习训练样本扩增同时修改标签名称
- 明明设计的是高带宽,差点加工成开路?
- 电视机尺寸与观看距离
- Embedded Engineer Interview frequently asked questions
- 嵌入式工程师面试题3-硬件
- Shell脚本-case in 和正则表达式
- Principle and application of single chip microcomputer - principle of parallel IO port
- 中考体育项目满分标准(深圳、安徽、湖北)
- 又到年中,固定资产管理该何去何从?
- win7 pyinstaller打包exe 后报错 DLL load failed while importing _socket:参数错误
猜你喜欢

MATLAB【函数求导】

Share 7 books I read in the first half of 2022

动态代理

FreeRTOS learning easy notes

What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR

AVL树的理解和实现

What is the material of 16MnDR, the minimum service temperature of 16MnDR, and the chemical composition of 16MnDR

安装Oracle EE

R语言入门

如何一站式高效管理固定资产?
随机推荐
Nacos - 配置管理
Shell脚本-case in语句
内存大小端
FreeRTOS learning easy notes
Shell脚本-for循环和for int循环
AES简单介绍
TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘
Common interview questions for embedded engineers 2-mcu_ STM32
【C】 Summary of wrong questions in winter vacation
Interrupt sharing variables with other functions and protection of critical resources
Properties of 15MnNiNbDR low temperature vessel steel, Wugang 15MnNiDR and 15MnNiNbDR steel plates
【面试必刷101】链表
我想知道手机注册股票开户的流程?另外,手机开户安全么?
Yolov3, 4, 5 and 6 Summary of target detection
SPL Introduction (I)
中考体育项目满分标准(深圳、安徽、湖北)
AVL树的理解和实现
爬虫知识点总结
《单片机原理及应用》-片外拓展
MySQL8.0学习记录17 -Create Table