shell循环结构之for循环

简介:

for循环


语法:


1)


for 变量 in 取值列表; do

statement

statement

done


2)


for 变量 in 取值列表 

do

statement

statement

done 



取值列表:

数字

10 20 30

使用seq命令生成数字的序列

seq 10

seq 3 10

seq 1 2 10

字符

aa bb cc 

文件



编写脚本,计算1---100的和


#!/bin/bash

#


sum=0


for i in `seq 100`; do

   let sum=$sum+$i

done


echo $sum



编写脚本,计算1--100所有奇数的和


#!/bin/bash

#


sum=0


for i in `seq 100`; do

   let ys=$i%2

   if [ $ys -ne 0 ]; then

      let sum=$sum+$i

   fi

done


echo $sum



编写脚本,实现批量创建用户user1...user10


#!/bin/bash

#


for i in `seq 10`; do

   if ! id user$i &> /dev/null; then

        useradd user$i

        echo "redhat" | passwd --stdin user$i &> /dev/null

        echo "用户user$i创建完成,初始密码为redhat"

   else

        echo "用户user$i已经存在"

   fi

done



编写脚本,检测172.16.20.0/24在线主机IP


#!/bin/bash

#


network=172.16.20.


for i in `seq 254`; do

   if ping -c 1 -W 1 $network$i &> /dev/null; then

echo "Host $network$i is up"

   fi

done



启动多线程:


#!/bin/bash

#


network=172.16.80.


for i in `seq 254`; do

    {

    if ping -c 1 -W 1 $network$i &> /dev/null; then

       echo "Host $network$i is up"

    fi

    }&

done

wait



以字符作为取值列表 


#!/bin/bash

#


for name in alice natsha kira merry; do

   useradd $name

   echo "$name create finished."

done



以文件作为取值列表 


`cat FILE`



编写脚本,向系统中所有用户打招呼  


#!/bin/bash

#


for i in `awk -F: '{print $1}' /etc/passwd`; do

echo "Hello, $i"

done



编写脚本, 将某目录下所有以.txt结尾的文件改为以.mp3结尾 


#!/bin/bash

#


for i in `ls -l /tmp/test | grep "txt$" | awk '{print $9}'`; do

    file_name=`echo $i | awk -F. '{print $1}'`

    mv /tmp/test/$i /tmp/test/$file_name.mp3

done



编写脚本, 分别统计/bin/bash和/sbin/nologin用户的个数 


#!/bin/bash

#


bash_number=0

nologin_number=0


line=$(wc -l /etc/passwd | awk '{print $1}')


for i in `seq $line`; do

  sh_name=$(head -n $i /etc/passwd | tail -1 | awk -F: '{print $7}')

  case $sh_name in

    /sbin/nologin)

        let nologin_number=$nologin_number+1

        ;;

    /bin/bash)

        let bash_number=$bash_number+1

        ;;

   esac

done


echo "shell为/bin/bash的:$bash_number"

echo "shell为/sbin/nologin的:$nologin_number"


编写脚本,显示九九乘法表 

  1. #!/bin/bash  

  2. #for嵌套for循环  

  3. #9*9乘法口诀  

  4. echo "for的九九乘法表"  

  5. for(( i=1 ;i<=9;i++ ))  

  6. do  

  7.    for(( j=1;j<=9;j++ ))  

  8.    do  

  9.       #当$j小于等于$i,在屏幕上打印乘法表  

  10.       [ $j -le $i ] && echo -n "$i*$j=$[ $i * $j ]  "  

  11.    done  

  12.    echo ''  

  13. done


中断循环的语句

break 中断整体循环

continue 中断本次循环




break用法:


#!/bin/bash

#


sum=0


for i in `seq 100`; do

   let sum=$sum+$i

   if [ $sum -ge 3000 ]; then

echo $i

break

   fi

done



continue用法:


#!/bin/bash

#


sum=0


for i in `seq 100`; do

   let ys=$i%2

   if [ $ys -eq 0 ]; then

continue

   fi

   let sum=$sum+$i

done


echo $sum



编写脚本,列出系统中shell为/bin/bash的前5个用户名 


#!/bin/bash

#


number=0

line=$(wc -l /etc/passwd | awk '{print $1}')


for i in `seq $line`; do

   sh_name=`head -n $i /etc/passwd | tail -1 | awk -F: '{print $7}'`

   if [ $sh_name == "/bin/bash" ]; then

        name=`head -n $i /etc/passwd | tail -1 | awk -F: '{print $1}'`

        echo -n "$name  "

        let number=$number+1

        if [ $number -eq 5 ]; then

                break

        fi

   fi

done

echo










本文转自 北冥有大鱼  51CTO博客,原文链接:http://blog.51cto.com/lyw168/1957413,如需转载请自行联系原作者
目录
相关文章
|
27天前
|
存储 运维 Shell
shell中for while until 三种循环的用法
shell编程中,有几种常见的循环结构,包括for循环、while循环和until循环,总的来说,循环shell编程中扮演着至关重要的角色,它们使得自动化任务变得更加容易,提高了效率,并且可以处理各种各样的编程需求。
251 13
shell中for while until 三种循环的用法
|
5月前
|
Shell
在Shell脚本中,`for`循环
在Shell脚本中,`for`循环
30 2
|
7月前
|
Shell
shell里的for循环详解
shell里的for循环详解
102 0
|
7月前
|
Shell
shell里的while循环详解
shell里的while循环详解
86 0
|
7月前
|
Shell
shell脚本里的循环
shell脚本里的循环
59 0
|
27天前
|
监控 Shell
shell学习(五) 【循环控制continue,break、while 语法】
shell学习(五) 【循环控制continue,break、while 语法】
12 0
|
3月前
|
Shell
shell脚本for循环复杂用法
shell脚本for循环复杂用法
49 5
|
3月前
|
算法 Shell Linux
Linux的shell命令——判断与循环
Linux的shell命令——判断与循环
39 1
|
4月前
|
Shell C语言 Perl
Shell 编程快速入门 之 循环结构详解
Shell 编程快速入门 之 循环结构详解
65 0
Shell 编程快速入门 之 循环结构详解
|
5月前
|
Shell
在Shell(如Bash)中,`while`循环
在Shell(如Bash)中,`while`循环
48 2