当前位置:网站首页>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
边栏推荐
- Installing Oracle EE
- 【MFC开发(17)】高级列表控件List Control
- Differences among tasks, threads and processes
- Shell脚本-数组定义以及获取数组元素
- 爬虫知识点总结
- 《单片机原理及应用》—定时器、串行通信和中断系统
- Programming with C language: calculate with formula: e ≈ 1+1/1+ 1/2! …+ 1/n!, Accuracy is 10-6
- [deep analysis of C language] - data storage in memory
- NIO-零拷贝
- Do you know how data is stored? (C integer and floating point)
猜你喜欢

Advanced level of C language pointer (Part 1)

FreeRTOS学习简易笔记

Li Kou 1358 -- number of substrings containing all three characters (double pointer)

《单片机原理及应用》-片外拓展

截图小妙招

动态代理

性能提升2-3倍!百度智能云第二代昆仑芯服务器上线

MD文档中插入数学公式,Typora中插入数学公式

TypeError: __ init__ () got an unexpected keyword argument ‘autocompletion‘

Computer tips
随机推荐
R语言观察日志(part24)--初始化设置
Foundation: 3 Opencv getting started images and videos
AES简单介绍
Principle and application of single chip microcomputer - off chip development
Mavros sends a custom topic message to Px4
What is the material of 15CrMoR, mechanical properties and chemical analysis of 15CrMoR
Bimianhongfu queren()
DataBinding源码分析
中考体育项目满分标准(深圳、安徽、湖北)
Shell脚本-位置参数(命令行参数)
What is 1cr0.5mo (H) material? 1cr0.5mo (H) tensile yield strength
用C语言编程:用公式计算:e≈1+1/1!+1/2! …+1/n!,精度为10-6
It is designed with high bandwidth, which is almost processed into an open circuit?
Nacos - 配置管理
软件工程师面试刷题网站、经验方法
Glitch Free时钟切换技术
Yolov3, 4, 5 and 6 Summary of target detection
Common interview questions for embedded engineers 2-mcu_ STM32
Glitch free clock switching technology
LogBack