shell编程_for_in_循环

for in格式
for无$变量in字符串
do
$变量
done
一简单的字符串枚举遍历法,利用for in格式对字符串按空格切份的功能
SERVICES="8022251108000232021 3306"
for x in$SERVICES
do
iptables-A INPUT-p tcp--dport$x-m state--state NEW-j ACCEPT
done
for variable in values--------字符串数组依次赋值
#!/bin/sh
for i in a b c字符串列表A B C
字符串用空格分隔,没有括号,没有逗号,然
后循环将其依次赋给变量i
变量没有$
do
echo"i is$i"
done
[macg@machome~]$sh test.sh
i is a
i is b
i is c
for in里,变量和*不等价
#!/bin/bash
for i in*.h;
do
cat${i}.h
done
[macg@vm test]$./tip.sh
cat:*.h.h:No such file or directory
$i代表的是整个路径,而不是*.h里的.h前面的部分改正
#!/bin/bash
for i in*.h
do
cat$i
done
[macg@vm test]$echo hahaha>>1.h
[macg@vm test]$echo ha>>2.h
[macg@vm test]$./tip.sh
hahaha
ha
例2:
for i in
/etc/profile.d/*.sh do
$i
done
$i代表的是
/etc/profile.d/color.sh, /etc/profile.d/alias.sh, /etc/profile.d/default.sh
for in对(命令行,函数)参数遍历
test()
{
local i
for i in$*;do
echo"i is$i"
done
}
$*是字符串:以"参数1参数2..."形式保存所有参数$i是变量i的应用表示
[macg@machome~]$sh test.sh p1p2p3p4
i is p1
i is p2
i is p3
i is p4
for in语句与通配符*合用,批量处理文件
批量改文件名
[root@vm testtip]#ls
aaa.txt ccc.txt eee.txt ggg.txt hhh.txt jjj.txt lll.txt nnn.txt
bbb.txt ddd.txt fff.txt go.sh iii.txt kkk.txt mmm.txt ooo.txt
[root@vm testtip]#cat go.sh
for i in*.txt*.txt相当于一个字符串数组,依次循环赋值给i
do
mv"$i""$i.bak"
done
[root@vm testtip]#sh go.sh
[root@vm testtip]#ls
aaa.txt.bak ccc.txt.bak eee.txt.bak ggg.txt.bak hhh.txt.bak jjj.txt.bak lll.txt.bak nnn.txt.bak
bbb.txt.bak ddd.txt.bak fff.txt.bak go.sh iii.txt .bak kkk.txt.bak mmm.txt.bak ooo.txt.bak
for in语句与``和$()合用,利用``或$()的将多行合为一行的缺陷,实际是合为一个字符串数组
for i in$(ls*.txt)
do
echo$i
done
[macg@machome~]$sh test
111-tmp.txt
111.txt
22.txt
33.txt
或者说,利用for in克服``和$()的多行合为一行的缺陷
利用for in自动对字符串按空格遍历的特性,对多个目录遍历LIST="rootfs usr data data2"
for d in$LIST;do
mount/backup/$d
rsync-ax--exclude fstab--delete/$d//backup/$d/ umount/backup/$d
done。

合集下载

shell脚本之循环使用forwhile详解

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待更新。

linux中for循环的使用

linux中for循环的使用

============ -_- ==============for num in $(seq 1 100)
10、LIST="rootfs usr data data2"
for d in $LIST; do
用for in语句自动对字符串按空格遍历的特性,对多个目录遍历
11、for i in {1..10}
18.for File in /proc/sys/net/ipv4/conf/*/accept_redirects; do
19.echo $File
20.done
21.echo "直接指定循环内容"
22.for i in f1 f2 f3 ;do
23.echo $i
24.done
25.echo
2.
for((i=1;i<10000000;i++));do
echo $i
done
3.
i=1
while(($i<10000000));do
echo $i
i=`expr $i + 1`
done
因为本方法调用expr故运行速度会比第1,第2种慢不少不过可稍作改进,将i=`expr $i + 1`改为i=$(($i+1))即可稍作速度的提升,不过具体得看相应shell环境是否支持
在shell用for循环做数字递增的时候发现问题,特列出shell下for循环的几种方法:
1.
for i in `seq 1 1000000`;do
echo $i
done
用seq 1 10000000做递增,之前用这种方法的时候没遇到问题,因为之前的i根本就没用到百万(1000000),因为项目需要我这个数字远大于百万,发现用seq 数值到 1000000时转换为1e+06,根本无法作为数字进行其他运算,或者将$i有效、正确的取用,遂求其他方法解决,如下

shellfor循环、循环变量值付给其他shell脚本的方法

shellfor循环、循环变量值付给其他shell脚本的方法

shellfor循环、循环变量值付给其他shell脚本的⽅法本⽂主要将在shell中如何编写for循环,并将循环变量作为下个shell脚本的参数。

shell for 循环:#!第⼀种写法类似C、Javafor ((i=1; i<=100; i ++))doecho $idone#!第⼆种写法 in应⽤for i in {1..100}doecho $idone#!第三种写法 seq 使⽤for i in `seq 1 100`doecho $idone将循环变量赋值到下⼀个脚本:在运⾏shell脚本时候,有三种⽅式来调⽤外部的脚本,exec(exec script.sh)、source(source script.sh)、fork(./script.sh)1、exec(exec /home/script.sh):使⽤exec来调⽤脚本,被执⾏的脚本会继承当前shell的环境变量。

但事实上exec产⽣了新的进程,他会把主shell的进程资源占⽤并替换脚本内容,继承了原主shell的PID号,即原主shell剩下的内容不会执⾏。

2、source(source /home/script.sh)使⽤source或者“.”来调⽤外部脚本,不会产⽣新的进程,继承当前shell环境变量,⽽且被调⽤的脚本运⾏结束后,它拥有的环境变量和声明变量会被当前shell保留,类似将调⽤脚本的内容复制过来直接执⾏。

执⾏完毕后原主shell继续运⾏。

3、fork(/home/script.sh)直接运⾏脚本,会以当前shell为⽗进程,产⽣新的进程,并且继承主脚本的环境变量和声明变量。

执⾏完毕后,主脚本不会保留其环境变量和声明变量。

#!main.sh主体#!/bin/sha=mainecho "a is $a"echo "PID for parent before 2.sh:$$"case $1 inexec)echo "using exec"exec ./2.sh ;;*)echo "using sourcing"source ./2.sh ;;esacecho "PID FOR parent after 2.sh :$$"echo "now m"#!2.sh#!/bin/shecho "PID FOR 2.SH:$$"echo "2.sh get a from main.sh is $a"a=2.shexport ab=3.shecho "now 2.sh a is $a"a is mainPID for parent before 2.sh:1162using sourcingPID FOR 2.SH:11622.sh get a from main.sh is main`这⾥写代码⽚`now 2.sh a is 2.shPID FOR parent after 2.sh :1162now m通过for循环,循环变量作为2.sh变量赋值并执⾏。

shell中for循环的执行步骤

shell中for循环的执行步骤

shell中for循环的执行步骤 Shell 中 for 循环的执行步骤
1. 初始化
先确定循环的控制变量和循环体的范围。

设置控制变量为循环起始值。

2. 测试循环条件
评估循环条件,确定循环是否应执行。

如果条件为真,则执行循环体。

如果条件为假,则跳出循环。

3. 执行循环体
执行循环体中的命令。

循环体可以包含任何有效的 shell 命令。

4. 更新控制变量
更新控制变量,通常是递增或递减一个单位。

更新后的控制变量应符合循环条件。

5. 回到步骤 2
如果满足循环条件,则重复步骤 2 至 4。

如果不满足循环条件,则循环结束。

附加说明:
for 循环的参数:do...done 语句中的 do 和 done 之间定义了循环体。

控制变量:控制变量通常是局部变量,仅在 for 循环内部可用。

循环条件:循环条件通常是一个测试表达式,例如 [ $i -lt 10 ]。

循环更新:循环更新通常是一个递增或递减运算,例如(($i++)) 或 (($i--))。

嵌套循环:可以在一个 for 循环中嵌套另一个 for 循环。

其他注意事项:
for 循环可以按顺序或逆序执行,具体取决于控制变量更新的方式。

可以使用 break 语句显式跳出循环。

可以使用 continue 语句跳过循环体的剩余部分,并继续下一次迭代。

for 循环还可以使用 in 关键字对数组或序列中的每个元素进行迭代。

linux shell 循环语句

linux shell 循环语句

linux shell 循环语句在Linux Shell中,循环语句允许我们重复执行命令或命令块,从而可以自动化一些任务。

一、for循环for循环可以遍历一系列参数,并对每个参数执行一系列的命令。

语法如下:```for 变量名 in 参数1 参数2 ... 参数ndo执行的命令或命令块done```具体实现方法如下:例1:遍历列表中的元素假设我们有如下一个列表:`(apple,banana,watermelon,orange)`,现在我们想要遍历所有元素并输出,使用for循环可以这样实现:```#!/bin/bashfruits=(apple banana watermelon orange)for fruit in ${fruits[@]}doecho $fruitdone```输出结果为:例2:从1到10的数字求和我们可以用for循环累加1到10的数字,实现求和功能:sum=0echo "1+2+...+10=$sum"```二、while循环while循环用于重复执行一系列命令,直到条件为假为止。

语法如下:如果循环条件不成立,则循环不会执行。

如果循环条件一直为真,则会一直执行循环中的命令。

例1:取余数大于2的数字下面的脚本会重复执行输入数字直到输入的数字取余数大于2为止。

echo "请输入一个数字: "read numwhile [ $(($num % 3)) -le 2 ]doecho "余数小于等于2"echo "请输入一个数字: "read numdoneecho "余数大于2,程序退出"```例2:执行命令直到成功下面的脚本会尝试执行一个命令,如果执行失败,则会一直尝试直到执行成功为止。

command="ls /not/exist/directory"while [ ! -d "/not/exist/directory" ]doecho "执行命令: $command"$commandif [ $? -ne 0 ]; thenecho "执行失败,将重试"sleep 3elseecho "执行成功"breakfidone```三、until循环until循环与while循环类似,但是它会一直执行循环直到条件成立(也就是为真)为止。

shell中的循环语句

shell中的循环语句

shell中的循环语句1. 嘿,你知道吗?Shell 中的循环语句就像是一个不知疲倦的小机器人,能帮咱们重复做很多事!比如说,要打印 1 到 10 这几个数,用for 循环就能轻松搞定:for ((i=1; i<=10; i++)); do echo $i; done 。

这难道不神奇吗?2. 亲,想象一下,Shell 里的循环语句是你的得力助手!就像一个超级听话的小伙伴,你让它干啥就干啥。

比如你想把一个文件夹里的所有文件名字都列出来,用 while 循环就能实现:ls | while read file; do echo $file; done 。

是不是很方便呀?3. 朋友,Shell 中的循环语句那可真是个宝贝!它就像一把万能钥匙,能打开好多复杂任务的大门。

比如说,要计算 1 到 100 的和,用 until循环:sum=0; i=1; until [ $i -gt 100 ]; do sum=$((sum + i)); i=$((i + 1)); done; echo $sum 。

这难道还不够厉害吗?4. 嗨呀!Shell 里的循环语句简直是神器!就好比一个永不停歇的小马达。

你看,要是想把一个数组里的元素逐个打印出来,用 for 循环就行:arr=(1 2 3 4 5); for item in "${arr[@]}"; do echo $item; done 。

是不是超简单?5. 亲,你想想,Shell 中的循环语句是不是像一个勤劳的小蜜蜂?不停歇地为我们工作。

比如要找出文件中包含特定字符串的行,用 while循环:grep 'keyword' file.txt | while read line; do echo $line; done 。

这多省事啊!6. 嘿,朋友!Shell 里的循环语句可是大功臣啊!如同一位忠实的卫士。

像要统计一个文件里有多少行,用 while 循环:count=0; while read line; do ((count++)); done < file.txt; echo $count 。

Shell逐行读取文件的3种方法

Shell逐⾏读取⽂件的3种⽅法⽅法1:while循环中执⾏效率最⾼,最常⽤的⽅法。

while read line
do
echo $line
done < filename
注释:这种⽅式在结束的时候需要执⾏⽂件,就好像是执⾏完的时候再把⽂件读进去⼀样。

⽅法2 :管道法: cat $FILENAME | while read LINE
cat filename | while read line
do
echo $line
done
注释:当遇见管道的时候管道左边的命令的输出会作为管道右边命令的输⼊然后被输⼊出来。

⽅法3 for 循环。

for line in `cat filename`
do
echo ${line}
done
注释:这种⽅式是通过for循环的⽅式来读取⽂件的内容相⽐⼤家很熟悉了,这⾥不多说。

在各个⽅法中,for语句效率最⾼,⽽在while循环中读写⽂件时,第⼀种⽅式执⾏效率最⾼。

for逐⾏读和while逐⾏读是有区别的,如:
$ cat t.txt
1111
2222
3333 4444 555
$ cat t.txt | while read line; do echo ${line}; done
1111
2222
3333 4444 555
$ for line in `cat t.txt`; do echo ${line}; done
1111
2222
3333
4444
555。

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中循环用法

shell中循环用法在Shell脚本中,可以使用以下几种循环用法:1. for循环:```shellfor 变量 in 列表do循环体done```示例:```shellfor i in {1..5}doecho "循环次数: $i"done```2. while循环:```shellwhile [ 条件 ]do循环体done```示例:```shelli=1while [ $i -le 5 ]doecho "循环次数: $i"i=$((i+1))done```3. until循环:```shelluntil [ 条件 ]do循环体done```示例:```shelli=1until [ $i -gt 5 ]doecho "循环次数: $i"i=$((i+1))done```4. select循环(用于菜单选择): ```shellselect 变量 in 列表do循环体done```示例:```shellselect fruit in Apple Banana Orange Exit docase $fruit inApple)echo "You selected Apple.";;Banana)echo "You selected Banana.";;Orange)echo "You selected Orange.";;Exit)break;;*)echo "Invalid selection.";;esacdone```这些循环用法可以根据具体需求选择使用,可以根据条件或固定次数来控制循环执行,并在循环体内执行相应的操作。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
相关文档
最新文档