前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >for循环、while循环、continue、break、exit解析、select用法

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

作者头像
阿dai学长
发布2019-04-03 11:03:15
1.2K0
发布2019-04-03 11:03:15
举报
文章被收录于专栏:阿dai_linux阿dai_linux

20.10 for循环

eg:

  • 求1到100数字的和。
代码语言:javascript
复制
[root@localhost sbin]# vim sum.sh
#!/bin/bash
sum=0
for i in `seq 1 5`
do
  sum=$[sum+$i]
done
echo "$sum"

[root@localhost sbin]# sh sum.sh 
15
  • 文件列表循环
代码语言:javascript
复制
[root@localhost 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!"

[root@localhost 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——分隔符
代码语言:javascript
复制
[root@localhost 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中间有一个空格)

[root@localhost 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秒执行一次。
代码语言:javascript
复制
[root@localhost 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" abc@111.com
  fi
   sleep 30
done
#while “:”表示死循环,也可以写成while true,意思是“真”(数学--真命题、假命题)

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


[root@localhost 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使用。

  • 交互模式下,用户输入一个字符,检测该字符是否符合条件,如:空、非数字、数字。分别对字符做出判断,然后做出不同的回应。
代码语言:javascript
复制
[root@localhost 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循环结束

[root@localhost 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:

代码语言:javascript
复制
[root@localhost 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!"


[root@localhost sbin]# sh break.sh 
1
1
2
2
3
Finished!

即,跳出while循环,继续执行循坏之外的命令。

20.14 continue 结束本次循环

eg:

代码语言:javascript
复制
[root@localhost 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!"


[root@localhost sbin]# sh continue.sh 
1
1
2
2
3
4
4
5
5
Finished!

即,结束本次循环之后重新开始下一次循环。

20.15 exit退出整个脚本

eg:

代码语言:javascript
复制
[root@localhost 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


[root@localhost sbin]# sh exit.sh 
1
1
2
2
3

退出整个脚本,后面的命令不会执行。

扩展:shell中select的用法

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

代码语言:javascript
复制
#!/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

执行结果如下:

代码语言:javascript
复制
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              LOGIN@   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即可,再次修改脚本如下:

代码语言:javascript
复制
#!/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

如果想要脚本每次输入一个序号后就自动退出,则需要再次更改脚本如下:

代码语言:javascript
复制
#!/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

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017/09/14 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 20.10 for循环
    • eg:
    • 20.11-20.12 while循环
      • eg:
      • 20.13 break 跳出循环
        • eg:
        • 20.14 continue 结束本次循环
          • eg:
          • 20.15 exit退出整个脚本
            • eg:
            • 扩展:shell中select的用法
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档