shell循环结构之while循环

简介:

while循环 


1) 


while CONDITION; do

statement

statement

<改变循环条件真假的语句>

done 



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


#!/bin/bash

#


sum=0

i=1


while [ $i -le 100 ]; do

   let sum=$sum+$i

   let i=$i+1

done


echo $sum



2) 


while true; do 

statement

statement

done 



#!/bin/bash

#


while true; do

   read -p "请输入你的选择:" choice

   if [ $choice == "q" ]; then

     break

   fi

done



#!/bin/bash

#


while true; do

   uptime

   sleep 3

done



3)


while read line; do 

statement

statement

done < file 



#!/bin/bash

#


bash_num=0

nologin_num=0


while read line; do

   sh_name=$(echo $line | awk -F: '{print $7}')

   case $sh_name in

        /bin/bash)

           let bash_num=$bash_num+1

           ;;

        /sbin/nologin)

           let nologin_num=$nologin_num+1

           ;;

   esac

done < /etc/passwd


echo $bash_num

echo $nologin_num





util循环:



util CONDITION; do 

    statement

statement

done 


条件为假时,执行循环,条件为真时,结束循环










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