菜单 学习猿地 - LMONKEY

VIP

开通学习猿地VIP

尊享10项VIP特权 持续新增

知识通关挑战

打卡带练!告别无效练习

接私单赚外块

VIP优先接,累计金额超百万

学习猿地私房课免费学

大厂实战课仅对VIP开放

你的一对一导师

每月可免费咨询大牛30次

领取更多软件工程师实用特权

入驻
421
0

for 循环

原创
05/13 14:22
阅读数 52281
  • 使用while循环读取文件
      cat file.txt |while read line
    do
    echo $line
    done

    或者:

    while read line
    do
    echo $line
    done < file.txt

注意:由于使用while来读入文件里的行时,会整行读入,不会关注行的内容(空格..),所以比for读文件有更好的适用性,推荐使用while循环读取文件

 

linux shell循环示例

for循环示例

for循环语法:

 

1

2

3

4

5

6


for VARIABLE in 1 2 3 4 5 .. N

do

         command1

         command2

         commandN

done


1

2

3

4

5

6

7

8

9

10

<i>#!/bin/bash

for i in 1 2 3 4 5

do

echo "Welcome $i times"

done

</i>


bash version 3.0+版本

 

#!/bin/bash

 for i in {1..5}

do

   echo "Welcome $i times"

done


bash version 4版本

 

#!/bin/bash

echo "Bash version ${BASH_VERSION}..."

for i in {0..10..2}

  do

     echo "Welcome $i times"

 done


含有“seq”命令的语法示例

 

#!/bin/bash

for i in $(seq 1 2 20)

do

   echo "Welcome $i times"

done


for循环的三个表达式

语法如下:

 

for (( EXP1; EXP2; EXP3 ))

do

         command1

         command2

         command3

done


示例如下:

#!/bin/bash

for (( c=1; c<=5; c++ ))

do

echo "Welcome $c times..."

done

效果:

 

Welcome 1 times

Welcome 2 times

Welcome 3 times

Welcome 4 times

Welcome 5 times


for的无限循环

 

#!/bin/bash

for (( ; ; ))

do

   echo "infinite loops [ hit CTRL+C to stop]"

done


break条件语句

 

for I in 1 2 3 4 5

do

  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.

  statements2

  if (disaster-condition)

  then

         break                #Abandon the loop.

  fi

  statements3          #While good and, no disaster-condition.

done


下面的shell脚本将通过在/ etc目录中存储的所有文件。 for循环将放弃当/ etc / resolv.conf的文件中找到。

 

#!/bin/bash

for file in /etc/*

do

         if [ "${file}" == "/etc/resolv.conf" ]

         then

                 countNameservers=$(grep -c nameserver /etc/resolv.conf)

                 echo "Total  ${countNameservers} nameservers defined in ${file}"

                 break

         fi

done


continue条件语句

 

for I in 1 2 3 4 5

do

  statements1      #Executed for all values of ''I'', up to a disaster-condition if any.

  statements2

  if (condition)

  then

         continue   #Go to next iteration of I in the loop and skip statements3

  fi

  statements3

done


利用这个脚本在命令行中指定的所有文件名的备份。如果。bak文件存在,它会跳过cp命令。

 

#!/bin/bash

FILES="$@"

for f in $FILES

do

        # if .bak backup file exists, read next file

         if [ -f ${f}.bak ]

         then

                 echo "Skiping $f file..."

                 continue  # read next file and skip cp command

         fi

        # we are hear means no backup file exists, just use cp command to copy file

         /bin/cp $f $f.bak

done

 

linux中shell编程for in循环语句的用法:
for in语句的格式:
for 无$变量 in 字符串
do
$变量
done
一简单的字符串枚举遍历法,利用for in格式对字符串按空格切份的功能
SERVICES="22 80 25 110 8000 23 20 21 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 ;#将list设置为当前目录下pwd的所有.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
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 p1 p2 p3 p4
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

发表评论

0/200
421 点赞
0 评论
收藏