shell中for循环用法
shell脚本之循环使用forwhile详解

shell脚本之循环使⽤forwhile详解任何⼀种编程语⾔中循环是⽐不可少的,当然 shell 脚本也少不了循环语句,包括 for 语句、 while 语句。
⽂中主要以实际⽤例来说明 for while 都有哪些常见的使⽤⽅法和技巧。
⼀、for 循环使⽤1. seq 命令⽅式for i in $(seq10);doecho"$i"done2. C语⾔语法⽅式for ((i=0; i<9; i++));doecho"$i"done3. 循环遍历⽂件列表for file in $(ls /root/); doecho"$file"done4. 循环遍历数组元素for i in ${arr[@]}; doecho"$i"done5. 循环遍历脚本参数列表for arg in $*; doecho"$arg"done6. 显式指定列表for i in123 ;doecho $idone7. 列表 for 循环for i in {1..5}; doecho"$i"done8. 步数跳跃⽅式进⾏循环for i in {1..20..2};doecho"$i"done⼆、while 循环使⽤1. 普通 while 语法while(( i<=100 ));do... ...donewhile [[ "$num" != 4 ]]; do... ...donewhile [ "$num" -gt 0 ]; do......done待更新。
Linuxshell实现用for循环100次的方法

C语言风格
for ((i=1; i<=100; i++)) do
echo $i done
Python风格(in的使用)
for i in {1..100} do
Байду номын сангаасecho $i done
Seq的使用
注意代码中不是单引号。
for i `seq 1 100` do
echo $i done
以上这篇Linux shell 实现用for循环100次的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多 支持。
这篇文章主要介绍编写一个awk脚本来找到一组单词中出现次数最多和最少的单词频率有需要的朋友可以借鉴参考下希望能够有所帮助祝大家多多进步早日升职加薪
Linuxshell实现用 for循环 100次的方法
前言
循环不管在程序中还是脚本中都需要经常用到,在写shell脚本时,经常需要for进行100次循环。这里谈谈几种从1到100的循 环方法。
shell for continue的用法

shell for continue的用法在Shell编程中,我们经常需要使用循环结构来重复执行一段代码。
其中,continue语句是一个非常有用的关键字,可以用于跳过当前循环中剩余的代码,直接进入下一轮循环的开始。
下面将介绍如何使用Shell的continue语句。
在Shell脚本中,continue语句通常与if语句和循环语句(如for循环和while循环)一起使用。
当某个条件成立时,我们可以使用continue语句跳过剩余的代码,返回循环的开头重新开始下一轮循环。
下面是一个示例,展示如何在for循环中使用continue语句:```shell#!/bin/bashfor (( i=1; i<=10; i++ ))doif (( i % 2 == 0 ))thencontinuefiecho "当前数字为:$i"done```在上面的示例中,我们使用了for循环来迭代变量i从1到10。
如果i能被2整除,也就是偶数,那么执行continue语句,直接跳过下面的echo语句,返回循环的开头开始下一轮循环。
如果i不能被2整除,也就是奇数,那么输出当前数字i。
运行以上示例脚本,将输出如下内容:```当前数字为:1当前数字为:3当前数字为:5当前数字为:7当前数字为:9```可以看到,当i为偶数时,continue语句起到了跳过echo语句的作用,只输出了奇数。
需要注意的是,continue语句只能用于循环结构中,例如for循环和while循环,并且只会跳过当前循环中的剩余代码,直接开始下一轮循环。
如果在没有嵌套循环的情况下使用continue语句,将会导致语法错误。
综上所述,Shell编程中的continue语句用于跳过当前循环中剩余的代码,直接进入下一轮循环的开始。
通过合理运用continue语句,我们可以在循环结构中实现更复杂的逻辑控制。
linux脚本遍历数组浅谈shell遍历数组的几种方法

linux脚本遍历数组浅谈shell遍历数组的几种方法在Linux脚本中,遍历数组是非常常见的操作。
在Shell中,有多种方法可以用来遍历数组。
下面将介绍几种常见的遍历数组的方法。
方法一:使用for循环遍历数组使用for循环可以遍历数组中的每个元素,并执行相应的操作。
下面是一个示例代码:```shell#!/bin/bash#定义一个数组array=("apple" "banana" "cherry" "date")# 使用for循环遍历数组doecho $itemdone```上述代码中,首先定义了一个名为`array`的数组,包含了四个元素。
然后使用`for`循环遍历数组中的每个元素,并通过`echo`命令打印出来。
方法二:使用for循环遍历数组的索引有时候,我们不仅需要遍历数组的元素,还需要获取其索引值。
可以通过使用`!`符号以及`#`符号来获取数组的长度和索引值。
下面是一个示例代码:```shell#!/bin/bash#定义一个数组array=("apple" "banana" "cherry" "date")# 使用for循环遍历数组的索引doecho "Index: $i, Value: ${array[$i]}"done```上述代码中,使用`for`循环从0开始,依次递增到数组的长度-1,通过`${array[$i]}`来获取数组中对应索引的值。
方法三:使用while循环遍历数组除了使用`for`循环,还可以使用`while`循环来遍历数组。
下面是一个示例代码:```shell#!/bin/bash#定义一个数组array=("apple" "banana" "cherry" "date")# 使用while循环遍历数组i=0doecho ${array[$i]}i=$((i+1))done```上述代码中,首先初始化一个变量`i`,然后使用`while`循环,判断`i`是否小于数组的长度,如果是则执行循环体内的操作,并将`i`递增。
shell中的for、while循环及if语句

shell中的for、while循环及if语句shell与其他语⾔⼀样也⽀持for、while循环for循环的⼀般格式如下:1 #!/bin/sh23for变量in列表4do5 command 16 command 27 command 18 .........9 command n10done列表是⼀组值(数字、字符串等)组成的序列,每个值通过空格分隔。
每循环⼀次,就将列表中的下⼀个值赋给变量。
列表也可以是⼀个⽂件:in 列表是可选的,如果不⽤它,for 循环使⽤命令⾏的位置参数。
1 #!/bin/sh23for line in12345674do5echo"line is $line"6done结果:[root@localhost 1st]# sh test.shline is 1line is 2line is 3line is 4line is 5line is 6line is 7下⾯⽤for循环读⼀个⽂件:查看⽂件内容1 [root@localhost 1st]# cat test.txt2 hello3 wolrd4 hello5 shell6 [root@localhost 1st]#code:1 #!/bin/sh23 FILE=./test.txt45for line in $(<$FILE)6do7echo"line is: $line"8doneResults:1 [root@localhost 1st]# sh xx.sh2 line is: hello3 line is: wolrd4 line is: hello5 line is: shell6 [root@localhost 1st]#while循环的⼀般格式如下:1while command2do34 statement56done3 i=04sum=05while [ $i -le 100 ]6do7sum=$(($sum + $i))8 i=$(($i + 1))9done10echo"sum is $sum"rusults:1 [root@localhost 1st]# sh xx.sh2sum is 50503 [root@localhost 1st]#if语句的⼀般格式如下:1if ....; then2 ....3elif ....; then4 ....5else6 ....7fiif 条件语句:⽂件表达式:1 [ -f "somefile" ] : 判断是否是⼀个⽂件2 [ -x "/bin/ls" ] : 判断/bin/ls是否存在并有可执⾏权限3 [ -n "$var" ] : 判断$var变量是否有值4 [ "$a" = "$b" ] : 判断$a和$b是否相等5 -r file ⽤户可读为真6 -w file ⽤户可写为真7 -x file ⽤户可执⾏为真8 -f file ⽂件为正规⽂件为真9 -d file ⽂件为⽬录为真10 -c file ⽂件为字符特殊⽂件为真11 -b file ⽂件为块特殊⽂件为真12 -s file ⽂件⼤⼩⾮0时为真13 -t file 当⽂件描述符(默认为1)指定的设备为终端时为真字符串表达式:[string string_operator string]这⾥string_operator可为如下⼏种:1 = 两个字符串相等。
shell for循环、循环变量值付给其他shell脚本

now a is 2.sh
73
a is 10
74
PID for parent before 2.sh:1339
75
using sourcing
76
PID FOR 2.SH:1339
77
2.sh get a from main.sh is 10
78
now 2.sh a is 2.sh
79
PID FOR parent after 2.sh :1339
08
echo "PID for parent before 2.sh:$$"
09
echo "using sourcing"
10
source ./2.sh
11
echo "PID FOR parent after 2.sh :$$"
12
echo "now a is $a"
13
done
输出结果:
01
a is 1
24
now a is 2.sh
25
a is 4
26
PID for parent before 2.sh:1339
27
using sourcing
28
PID FOR 2.SH:1339
29
2.sh get a from main.sh is 4
30
now 2.sh a is 2.sh
31
PID FOR parent after 2.sh :1339
本文主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。
shell for循环:
shell脚本中的4种循环语句使用

shell脚本中的4种循环语句使⽤1、for循环#语法结构#第⼀种:取值变量for变量名in变量取值表do指令done#例⼦:#⽰例for a in {1..9}domkdir dir$adone#第⼆种:C语⾔型for循环for ((exp1; exp2; exp3))do指令done#例⼦:#⽰例for ((i=1;i<=3;i++))doecho $idone#解释:i从1开始,当i<=3就可以运⾏,如果运⾏的值⼤于3,就退出循环#语法结构讲解for关键字后的双括号是三个表达式,第⼀个是变量初始化(例如:i=1),第⼆个为变量的范围(例如i<=3),第三个为变量⾃增或⾃减(例如i++)。
当第⼀个表达式的初始化值符合第⼆个变量的范围时,就进⾏如循环执⾏,当条件不满⾜时就退出循环#简单⽰例#1.竖向打印10 9 8 7 6 5⼏个数字#第⼀种⽅法:直接列出元素[root@game scripts]# cat for1.sh#!/bin/bashfor i in12345doecho $idone#效果[root@game scripts]# sh for1.sh12345第⼆种⽅法:使⽤⼤括号{}⽣成数字序列[root@game scripts]# cat for2.sh#!/bin/bashfor i in {1..5}doecho $idone#效果[root@game scripts]# sh for2.sh12345#第三种⽅法:使⽤seq⽣成数字序列[root@game scripts]# cat for3.sh#!/bin/bashfor i in `seq 15`doecho $idone#效果[root@game scripts]# sh for3.sh12345#2.获取当前⽬录下的⽬录或⽂件名,并将其作为变量列表打印输出#数据[root@game ~]# mkdir -p /test/{test1.txt,test2.txt,guo.txt,ke.txt}[root@game ~]# ls -l /test/total 0drwxr-xr-x. 2 root root 6 Aug 2122:14 guo.txtdrwxr-xr-x. 2 root root 6 Aug 2122:14 ke.txtdrwxr-xr-x. 2 root root 6 Aug 2122:14 test1.txtdrwxr-xr-x. 2 root root 6 Aug 2122:14 test2.txt#编写脚本[root@game scripts]# cat for4.sh#!/bin/bashusage(){echo "directory not found"}[ ! -d /test ] && usage && exit 1cd /testfor i in `ls`doecho $idone#效果[root@game scripts]# sh for4.shguo.txtke.txttest1.txttest2.txt2、while循环#while循环⼀般应⽤于守护进程程序或⼀直循环执⾏#语法格式while <条件表达式>do指令done#简单⽰例每隔2秒在屏幕上输出⼀次负载值[root@game scripts]# cat while1.shwhile truedouptimesleep 2 #暂停2秒再执⾏done#提⽰:while true表⽰条件永远为真,因此会⼀直运⾏,像死循环⼀样,称为守护进程#效果:每隔2秒就输出⼀次[root@game scripts]# sh while1.sh23:11:35 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.0523:11:37 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.0523:11:39 up 2 days, 2:00, 2 users, load average: 0.00, 0.01, 0.053、until循环#until循环是当条件表达式不成⽴时,就会进⼊循环,当条件表达式成⽴时,就会终⽌循环#语法格式until <条件表达式>do指令done#⽰例#如果⽤户输出的是guoke就符合条件,退出循环,如果不是,⽤户输⼊3次之后就退出循环[root@game scripts]# cat until1.sh#!/bin/bashi=1until [ "$user" = "guoke" -o "$i" -gt 3 ]doread -p "please enter you username:" userlet i++done#效果[root@game scripts]# sh until1.shplease enter you username:guoke[root@game scripts]# sh until1.shplease enter you username:1please enter you username:1please enter you username:1[root@game scripts]#4、select循环#语法格式select变量名in [菜单取值列表]do指令done#⽰例#第⼀种:直接使⽤列表字符串[root@game scripts]# cat select1.sh#!/bin/bashselect name in apache httpd nginx tomcatdoecho $namedone#效果[root@game scripts]# sh select1.sh1) apache2) httpd3) nginx4) tomcat#? 1#? 3nginx#? 4tomcat#? ^C#第⼆种:采⽤数组做变量列表[root@game scripts]# cat select2.sh#!/bin/basharray=(apache nginx tomcat lighttpd)select name in"${array[@]}"doecho $namedone#效果[root@game scripts]# sh select2.sh1) apache2) nginx3) tomcat4) lighttpd#? 3tomcat#? 4lighttpd#? ^C5.循环控制及状态返回值break (循环控制)continue (循环控制)exit (退出脚本)return (退出函数)#区别break continue在条件语句及循环语句(for if while等)中⽤于控制程序的⾛向exit是终⽌所有语句并退出脚本return:仅⽤于在函数内部返回函数执⾏的状态值#break⽰例#如果i等于3,那么就终⽌循环[root@game scripts]# cat break1.sh#!/bin/bashfor ((i=0;i<=5;i++))doif [ $i -eq 3 ];thenbreakelseecho $ifidoneecho "1111"yum install net-tools -y > /dev/null[ $? -eq 0 ] && echo "already install"#效果[root@game scripts]# sh break1.sh121111already install#说明:i等于3的时候就终⽌循环,但是没有跳出脚本#exit⽰例[root@game scripts]# cat exit1.sh#!/bin/bashfor ((i=0;i<=5;i++))doif [ $i -eq 3 ];thenexit 1fiecho $idoneecho "ok"#执⾏效果[root@game scripts]# sh exit1.sh12#说明:当i等于3的时候就会退出脚本了,就不会执⾏后⾯的语句#continue⽰例[root@game scripts]# cat con1.sh#!/bin/bashfor ((i=0;i<=5;i++))doif [ $i -eq 3 ];thencontinueelseecho $ifidoneecho "ok"#执⾏效果[root@game scripts]# sh con1.sh。
shell脚本for循环实现文件和目录遍历

shell脚本for循环实现⽂件和⽬录遍历⼀个for循环实现⼀个⽬录下的⽂件和⽬录遍历,很实⽤[root@localhost shell_order]# cat test27.sh#!/bin/bash#print the directory and filefor file in /home/hustyangju/*doif [ -d "$file" ]thenecho "$file is directory"elif [ -f "$file" ]thenecho "$file is file"fidone[root@localhost shell_order]# ./test27.sh/home/hustyangju/array is directory/home/hustyangju/menuwindow-7.12 is directory/home/hustyangju/menuwindow-build-desktop is directory/home/hustyangju/shell_order is directory[root@localhost shell_order]#递归遍历#! /bin/bashread_dir(){for file in `ls $1` #注意此处这是两个反引号,表⽰运⾏系统命令doif [ -d $1"/"$file ] #注意此处之间⼀定要加上空格,否则会报错thenread_dir $1"/"$fileelseecho $1"/"$file #在此处处理⽂件即可fidone}#读取第⼀个参数read_dir $1补充:Shell遍历⽬标⽬录和⼦⽬录下的所有⽂件1.编写代码#!/bin/bashfunction getdir(){for element in `ls $fd`dodir_or_file=$fd"/"$elementif [ -d $dir_or_file ]thengetdir $dir_or_fileelseecho $dir_or_filefidone}root_dir="/opt/datas"getdir $root_dir2.参数-e 判断对象是否存在-d 判断对象是否存在,并且为⽬录-f 判断对象是否存在,并且为常规⽂件-L 判断对象是否存在,并且为符号链接-h 判断对象是否存在,并且为软链接-s 判断对象是否存在,并且长度不为0-r 判断对象是否存在,并且可读-w 判断对象是否存在,并且可写-x 判断对象是否存在,并且可执⾏-O 判断对象是否存在,并且属于当前⽤户-G 判断对象是否存在,并且属于当前⽤户组-nt 判断file1是否⽐file2新 [ "/data/file1" -nt "/data/file2" ]-ot 判断file1是否⽐file2旧 [ "/data/file1" -ot "/data/file2" ]3.测试测试结果:打印出来了⽬标⽬录以及⼦⽬录下的所有⽂件到此这篇关于shell脚本for循环实现⽂件和⽬录遍历的⽂章就介绍到这了,更多相关shell⽂件和⽬录遍历内容请搜索以前的⽂章或继续浏览下⾯的相关⽂章希望⼤家以后多多⽀持!。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
shell中for循环用法
在Shell中,for循环用于重复执行一些命令或操作,根据给定的条件或范围进行迭代。
它是Shell编程中最常用和基本的控制结构之一
一般语法形式如下:
```
for 变量 in 列表
do
命令序列
done
```
其中,`变量`是循环变量,用于迭代遍历`列表`中的元素。
`命令序列`是在每次循环迭代时要执行的命令或操作。
下面我将详细介绍Shell中for循环的用法,包括列表的表示、循环变量的应用以及一些常见应用场景。
1.列表表示方法
列表表示在for循环中扮演着非常重要的角色,它决定了循环变量将遍历的元素集合。
a)直接定义
可以直接将元素列举在for循环中,用空格或换行符分隔。
例如:```
for color in red green blue
do
echo $color
done
```
输出结果:
```
red
green
blue
```
b)列表命令
可以使用Shell命令生成需要的列表,通过反引号或`$(`将命令包裹起来即可。
例如:
```
for file in $(ls *.txt)
do
echo $file
done
输出结果为当前目录下所有以`.txt`结尾的文件列表。
c)数字范围
可以使用`seq`命令生成数字范围作为列表。
例如:
```
for num in $(seq 1 5)
do
echo $num
done
```
输出结果为:12345
2.循环变量的应用
循环变量可用于处理每次循环迭代时的命令或操作,可以通过`$变量`的方式引用循环变量的值。
a)文件名遍历
```
for file in $(ls)
do
echo "File: $file"
```
输出结果为当前目录下的所有文件名。
b)运算表达式
可以在循环体中使用循环变量进行运算。
```
for i in $(seq 1 5)
do
result=$(expr $i \* 2)
echo "Result: $result"
done
```
输出结果为:246810
c)多变量循环
可以同时遍历多个列表。
```
for name in "Tom" "Jerry" "Bob"
do
for age in 18 20 25
echo "Name: $name, Age: $age"
done
done
```
输出结果为:Tom 18、Tom 20、Tom 25、Jerry 18...依此类推。
3.常见应用场景
a)批量重命名
```
for file in $(ls *.txt)
do
mv $file $(basename $file .txt).bak
done
```
将当前目录下所有的`.txt`文件扩展名修改为`.bak`。
b)执行命令
```
for i in $(seq 1 5)
do
echo "Message $i"
sleep 1
done
```
每隔1秒输出一条消息,共输出5次。
c)目录遍历
```
for dir in $(find /path/to/dir -type d)
do
echo "Directory: $dir"
done
```
遍历指定目录及其子目录下的所有目录。
综上所述,Shell中的for循环提供了灵活和方便的循环结构,通过遍历列表和应用循环变量,我们可以处理各种复杂的任务和应用场景。
对于Shell编程而言,掌握for循环用法无疑是非常重要的。