for循环、while循环、continue、break、exit解析、select用法

20.10 for循环

eg:

求1到100数字的和。
[[email protected] sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in seq 1 5
do
sum=$[sum+$i]
done
echo "$sum"

[[email protected] sbin]# sh sum.sh
15
文件列表循环
[[email protected] sbin]# vim for.sh
#!/bin/bash
dir=/usr/local/sbin/
for a in ls $dir
do
if [ -d $a ]
then
echo $a
ls $a
fi
done
echo "No directory file!"

[[email protected] sbin]# sh -x for.sh

  • dir=/usr/local/sbin/
    ++ ls /usr/local/sbin/
  • for a in ‘ls $dir
  • ‘[‘ -d datefile.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d filetar.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d for.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d if2.sh ‘]‘
  • for a in ‘ls $dir
  • ‘[‘ -d sum.sh ‘]‘
  • echo ‘No directory file!‘
    No directory file!
    for——分隔符
    [[email protected] adai]# ll
    总用量 0
    -rw-r--r-- 1 root root 0 9月 14 19:25 1
    -rw-r--r-- 1 root root 0 9月 14 19:25 2
    -rw-r--r-- 1 root root 0 9月 14 19:25 3 4.txt
    #注意:3 4.txt是一个文件(34中间有一个空格)

[[email protected] adai]# for i in ls ./; do echo $i ; done
1
2
3
4.txt
以上结果显示说明,for默认情况下把空格或换行符(回车)作为分隔符。

20.11-20.12 while循环

格式: while 条件;do…;done

eg:

当系统负载大于10的时候,发送邮件,每隔30秒执行一次。
[[email protected] adai]# vim while.sh
#!/bin/bash
while :
do
load=w|head -1 |awk -F ‘load average:‘ ‘{print $2}‘ |cut -d . -f1
if [ $load -gt 10 ]
then
top |mail -s "load is high: $load" [email protected]
fi
sleep 30
done
#while “:”表示死循环,也可以写成while true,意思是“真”(数学--真命题、假命题)

#Attention:awk -F ‘load average: ‘此处指定‘load average: ‘为分隔符,注意冒号后面的空格
#如果不加该空格,过滤出来的结果会带空格,需要在此将空格过滤掉

[[email protected] adai]# sh -x while.sh

  • :
    ++ head -1
    ++ awk -F ‘load average: ‘ ‘{print $2}‘
    ++ cut -d. -f1
    ++ w
  • load=0
  • ‘[‘ 0 -gt 10 ‘]‘
  • sleep 30
    .
    .
    .
    如果不手动停止该脚本,它会一直循环执行(按Ctrl+c结束),实际环境中配合screen使用。

交互模式下,用户输入一个字符,检测该字符是否符合条件,如:空、非数字、数字。分别对字符做出判断,然后做出不同的回应。
[[email protected] sbin]# vim while2.sh
#!/bin/bash
while true
do
read -p "Please input a number:" n
if [ -z "$n" ]
then
echo "You need input some characters!"
continue
fi
n1=echo $n|sed ‘s/[-0-9]//g‘
if [ -n "$n1" ]
then
echo "The character must be a number!"
continue
fi
break
done
echo $n
#continue:中断本次while循环后重新开始;
#break:表示跳出本层循环,即该while循环结束

[[email protected] sbin]# sh while2.sh
Please input a number:
You need input a character!
Please input a number:eee333
The character must be a number!
Please input a number:3
3
20.13 break 跳出循环

eg:

[[email protected] sbin]# vim break.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
break
fi
echo "$i"
done
echo "Finished!"

[[email protected] sbin]# sh break.sh
1
1
2
2
3
Finished!
即,跳出while循环,继续执行循坏之外的命令。

20.14 continue 结束本次循环

eg:

[[email protected] sbin]# vim continue.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
continue
fi
echo "$i"
done
echo "Finished!"

[[email protected] sbin]# sh continue.sh
1
1
2
2
3
4
4
5
5
Finished!
即,结束本次循环之后重新开始下一次循环。

20.15 exit退出整个脚本

eg:

[[email protected] sbin]# vim exit.sh
#!/bin/bash
for i in seq 1 5
do
echo "$i"
if [ $i -eq 3 ]
then
exit
fi
echo "$i"
done

[[email protected] sbin]# sh exit.sh
1
1
2
2
3
退出整个脚本,后面的命令不会执行。

扩展:shell中select的用法

select也是循环的一种,它比较适合用在用户选择的情况下。
比如,我们有一个这样的需求,运行脚本后,让用户去选择数字,选择1,会运行w命令,选择2运行top命令,选择3运行free命令,选择4退出。脚本这样实现:

#!/bin/bash

echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo
select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
;;
esac
done
执行结果如下:

sh select.sh
Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit

1) w
2) top
3) free
4) quit
#? 1
16:06:58 up 109 days, 22:01, 1 user, load average: 0.11, 0.05, 0.01
USER TTY FROM [email protected] IDLE JCPU PCPU WHAT
root pts/0 222.128.156.84 16:05 0.00s 0.00s 0.00s w

#? 3
total used free shared buffers cached
Mem: 1020328 943736 76592 0 86840 263624
-/+ buffers/cache: 593272 427056
Swap: 2097144 44196 2052948
#?
我们发现,select会默认把序号对应的命令列出来,每次输入一个数字,则会执行相应的命令,命令执行完后并不会退出脚本。它还会继续让我们再次输如序号。序号前面的提示符,我们也是可以修改的,利用变量PS3即可,再次修改脚本如下:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w
;;
top)
top
;;
free)
free
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4)."
esac
done
如果想要脚本每次输入一个序号后就自动退出,则需要再次更改脚本如下:

#!/bin/bash
PS3="Please select a number: "
echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
echo

select command in w top free quit
do
case $command in
w)
w;exit
;;
top)
top;exit
;;
free)
free;exit
;;
quit)
exit
;;
*)
echo "Please input a number:(1-4).";exit
esac
done

原文地址:http://blog.51cto.com/13242922/2105610

时间: 2024-08-11 07:34:51

for循环、while循环、continue、break、exit解析、select用法的相关文章

shell脚本编程之循环控制语句(continue/break/sleep)

循环控制语句: continue:提前结束本轮循环,而直接进入下一轮循环判断: while  CONDITION1; do CMD1 ... if  CONDITION2; then continue fi CMDn ... done 示例:求100以内所有偶数之和: #!/bin/bash # declare -i evensum=0 declare -i i=0 while [ $i -le 100 ]; do     let i++     if [ $[$i%2] -eq 1 ]; th

2018-4-19 17周2次课 for循环、while循环、break、continue、exit

20.10 for循环 ·语法:for 变量名 in 条件; do -; done 案例1 计算1到100数字的和 #!/bin/bash sum=0 for i in `seq 1 100` do     sum=$[$sum+$i]                ##核心语句     echo $i done echo $sum 案例2 列出/etc/下的目录或子目录 #!/bin/bash cd /etc/ for a in `ls /etc/` do     if [ -d $a ]  

shell 编程 (三)-for 循环,while循环,break,continue,exit

[toc] 一.for循环 重复执行一系列命令在 编程中很常见.通常你需要重复一组命令直到达到某个特定条件,比如处理某个目录下的所有文件.系统上的所有用户或者是某个文本文件中的所有行. 常见的两种循环,在脚本中普遍被用到. for循环while循环 语法:for 变量名 in 条件; do -; done for var in list do commands done 在list参数中,提供了迭代中要用的一系列值 示例1:用for循环来写个1-100的求和. 思路: [ ] 首先需要把1-10

for循环 while循环 break跳出循环 continue结束本次循环 exit退出整

一.for循环需求:计算1-100所有数字的和[[email protected] shell]# vi for1.sh#!/bin/bashfor i in seq 1 100doecho $i //先把100个数字打印出来done[[email protected] shell]# sh for1.sh //执行脚本打印出100个数字 继续修改脚本:[[email protected] shell]# vi for1.sh#!/bin/bashsum=0for i in seq 1 100d

for循环、while循环、break、continue、exit

1. for循环 语法:for 变量名 in 条件; do -; done案例1 #!/bin/bashsum=0for i in `seq 1 100`do# echo "$sum + $i" sum=$[$sum+$i]doneecho $sum 注意:for 在用于文件或目录列表的时候,它是以回车或空格为分隔符的(所以得注意文件名中不能有空格). 对文件相关的应用:   2.while循环 #!/bin/bashwhile :do load=`w|head -1|awk -F '

while与do while 区别 for循环的简介及break和continue的区别

do while 循环和while循环的区别   1.do while循环是先执行循环体,然后判断循环条件,如果为真,则执行下一步循环,否则终止循环:    while循环是先判断循环条件,如果条件为真则执行循环体:   2.do while循环条件后面必须有一个分号,这个分号表明循环结束. 1.for循环 for循环是更加简洁的循环语句,大部分情况下,for循环可以代替while循环.do-while循环. for循环的格式为: for( 初始语句  ; 执行条件  ; 增量 ) { 循环体

c:走出循环的几种方法之continue,break,goto,return

走出循环结构的几种方法:continue,break,goto,return 一:continue结束本次循环,直接进入下一次循环 int main(int argc, const char * argv[]) { for (int j = 0; j<10; j++) { if (j>5) { continue; } printf("%d\n",j); } return 0; } 打印结果: 012345 二:break结束整个循环结构. int main(int argc

一文了解Python中的循环(for while break continue 嵌套循环...)

循环 目标 程序的三大流程 while 循环基本使用 break 和 continue while 循环嵌套 01. 程序的三大流程 在程序开发中,一共有三种流程方式: 顺序 —— 从上向下,顺序执行代码 分支 —— 根据条件判断,决定执行代码的 分支 循环 —— 让 特定代码 重复 执行 02. while 循环基本使用 循环的作用就是让 指定的代码 重复的执行 while 循环最常用的应用场景就是 让执行的代码 按照 指定的次数 重复 执行 需求 —— 打印 5 遍 Hello Python

Linux shell break、continue、exit、return的用法

break.continue.exit.return一般用于控制循环的的走向 首先通过一个脚本说明 for ((i=1;i<5;i++ )) do if [ $i -eq 3 ] then #  break #  continue #  exit fi echo $i done echo OK 输出的结果 break的结果 1 2 OK continue的结果 1 2 4 OK exit的结果 1 2 由此可以说明 break n:n表示跳出循环的层数,如果省略n表示跳出整个循环 continu

break、continue、exit、return的区别和对比

一:说明 break.continue在条件循环语句及循环语句(for.while.if等)中用于控制程序的走向:而exit则用于种植所有语句并退出当前脚本,除此之外,exit还可以返回上一级程序或命令的执行状态值给当前shell:return类似于exit,只不过return仅用于在函数内部返回函数执行的状态值. break n 如果省略n,则表示跳出整个循环,n表示跳出循环的层数: continue n 如果省略n,则表示跳出本次循环,忽略本次循环的剩余代码,进入循环的下一次循环,n表示退到