当前位置:网站首页>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
边栏推荐
- 《单片机原理及应用》-片外拓展
- [deep analysis of C language] - data storage in memory
- Redis源码学习(29),压缩列表学习,ziplist.c(二)
- Public network cluster intercom +gps visual tracking | help the logistics industry with intelligent management and scheduling
- Brief introduction to AES
- 【面试必刷101】链表
- Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
- Common interview questions for embedded engineers 2-mcu_ STM32
- 19Mn6 German standard pressure vessel steel plate 19Mn6 Wugang fixed binding 19Mn6 chemical composition
- 3. Detailed explanation of Modbus communication protocol
猜你喜欢

MATLAB【函数求导】

Matlab tips (23) matrix analysis -- simulated annealing

Vscode customize the color of each area

Share 7 books I read in the first half of 2022

TypeError: __init__() got an unexpected keyword argument ‘autocompletion‘

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

如何一站式高效管理固定资产?

Glitch free clock switching technology

电脑小技巧

动态代理
随机推荐
In depth learning training sample amplification and tag name modification
Centos7 shell脚本一键安装jdk、mongo、kafka、ftp、postgresql、postgis、pgrouting
【MFC开发(17)】高级列表控件List Control
中断与其他函数共享变量、临界资源的保护
避免按钮重复点击的小工具bimianchongfu.queren()
基础:3.opencv快速入门图像和视频
Shell脚本-select in循环
FreeRTOS learning easy notes
【C】 Summary of wrong questions in winter vacation
Shell脚本-case in语句
如何一站式高效管理固定资产?
AES简单介绍
1. Connection between Jetson and camera
[MFC development (17)] advanced list control list control
Dynamic proxy
《微机原理》—总线及其形成
What is the material of 16mo3 steel plate? What is the difference between 16mo3 and Q345R?
Guidelines and principles of did
View drawing process analysis
Shell脚本-if else语句