云计算-shell编程之until循环,select循环,循环控制break continue

until循环语句

nutil循环与while循环类似,基于一个条件。
但是,until循环的的判断条件与while的正好相反,until循环在条件为假的情况下才会继续运行。
一旦条件为真,则退出循环

语法格式如下:

until  [ CONDITION ]
do
	command1
	command2
	......
	commandN
done

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
until与while对比:
  • until循环执行直到返回0状态
  • while循环执行直到返回非0状态
  • while循环执行直到返回非0状态
  • until循环总是执行至少一次

来看一个栗子:

!/bin/bash
#=====================================
#
#       AYTHOR:Xie_qi_chao
#       FILE:untilloop.sh
#       CREATED:18/03/2019
#
#=====================================

var=1

until [ $var -gt 3 ]
do
echo “The for loop is run $var times.”

    var=$(( var + 1 ))

done

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

此脚本运行结果如下:

[root@xieqc shell]# sh untilloop.sh
The for loop is run 1 times.
The for loop is run 2 times.
The for loop is run 3 times.

 
 
  • 1
  • 2
  • 3
  • 4
select 循环

来看一下语法格式:

select  VAR  in  LIST
do
	command1
	command2
	......
	commandN
done

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

特点如下:

  • select 语句使用bash内部变量PS3 的值作为它的提示符信息
  • 打印到屏幕上的列表LIST中的每一项会在前面加上一个数字编号
  • 当用户输入的数字与某一个数字编号一致时,列表中相应的项即被赋予变量VAR
  • 如果 用户输入的内容为空,将重新显示列表LIST中的项和提示符信息
  • 可以通过添加一个退出选项,或者Ctrl+C或者Ctrl+D组合键退出select循环

不解释,直接上脚本:

#!/bin/bash
#=======================================
#
#	AUTHOR:Xie_qi_chao
#	CREATED:19/03/2019  00:10
#	FILE:selectloop.sh
#
#=======================================

PS3="Run command: " #定义PS3提示符

select choice in date w hostname “uname -a” Exit #指定select循环的列表
do

case $choice in
    date)
    echo "==================================="
    echo "Current system date and time"
    echo "==================================="
    $choice   #直接将变量的值作为命令运行
    ;;
w)
    echo "==================================="
    echo "Who is ogged on and what they are doing"
    echo "==================================="
    $choice
    ;;
hostname)
    echo "==================================="
    echo "Hostname:"
    echo "==================================="
    $choice
    ;;
"uname -a")
    echo "==================================="
    echo "System information"
    echo "==================================="
    $choice
    ;;
Exit)
    echo "Bye!"
    exit
    ;;

esac

done

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47

执行的结果是这样的:

[root@xieqc shell]# sh selectloop.sh
1) date
2) w
3) hostname
4) uname -a
5) Exit
Run command: 1
===================================
Current system date and time
===================================
2019年 03月 18日 星期一 23:33:16 CST
Run command: 5
Bye!
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
循环控制:break continue语句
  • break语句停止 循环的执行
  • 语法格式 简单:break [n]
  • n是嵌套循环的层级,指定n,就n级循环嵌套退出,没指定,退出状态码为0,否则退出状态码为n

上菜:

#!/bin/bash
#=======================================
#
#	AUTHOR:Xie_qi_chao
#	CREATED:19/03/2019  00:45
#	FILE:forbreak.sh
#
#=======================================

#如果未指定参数,则打印脚本的使用方法,并返回退出状态码1
[ $# -eq 0 ] && { echo “Usage: $0 fikepath”; exit 1; }

#将位置参数1的值赋给变量match
match=$1
found=0

#便历目录/etc下所有的文件
for file in /etc/*
do

#如果文件的路径与指定的参数文件路径相匹配,则打印文件已找到,并退出for循环
if [ $file == "$match" ]
then

  echo "The file $match was found!"
  found=1
  #使用break命令退出for循环
  break
fi	

done

[ $found -ne 1 ] && echo “The file $match not found in /etc directory”

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

执行结果是这个 样子滴:

[root@xieqc shell]# sh forbreak.sh haha
The file haha not found in /etc directory

 
 
  • 1
  • 2
看一个嵌套的循环退出脚本实例:
#!/bin/bash
#=======================================
#
#	AUTHOR:Xie_qi_chao
#	CREATED:19/03/2019  01:20
#	FILE:breaknestedloop.sh
#
#=======================================

#如果未指定参数,则打印脚本的使用方法,并返回退出状态码1
[ $# -eq 0 ] && { echo “Usage: $0 fikepath”; exit 1; }

#将位置参数1的值赋给变量match
match=$1
found=0

for dir in /bin /usr/bin
do

#便历目录/etc下所有的文件
for  file  in  $dir/*
do

#如果文件的路径与指定的参数文件路径相匹配,则打印文件已找到,并退出for循环
if [ $(basename $file) == "$match" ]
then

  echo "The file $match was found!"
  found=1
  #使用break命令退出for循环
  break  2
fi	
done

done
[ $found -ne 1 ] && echo “The command $match not found”

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
countil 语句
  • 用于跳过循环体中剩余的命令直接跳转到循环体的顶部,而重新开始下一次重复
  • 意思是只结束当前本次的循环

将你指定的目录下所有大写字母的文件名,改成小写。

#!/bin/bash
#=======================================
#
#       AUTHOR:Xie_qi_chao
#       CREATED:19/03/2019  01:40
#       FILE:tolower.sh
#
#=======================================

#如果脚本未指定时,未指定参数,则打印脚本的使用方法,并返回退出状态码1
[ $# -eq 0 ] && { echo “Usage: $0 directory”; exit 1; }

#如果指定的目录不存在,则打印错误信息,并返回退出码1
[ ! -d $1 ] && { echo “Connot cd to the directory $1” ; exit 1; }

#如果没有成功切换到指定的目录,则打印相应的错误信息,并返回退出状态码 1
cd $1 || { echo “Connit cd to the directory $1”; exit 1; }

#遍历指定目录下的所有文件
for filename in $(ls)
do
#如果文件名不包含大小写字母,直接跳到下一次循环
if [ $filename != [[:upper:]] ]
then

#忽略 for循环体中的剩余语句直接跳到下一次循环
continue

fi

#将变量filename中的字母转换为小写
new=`echo $filename | tr 'A-Z'  'a-z' `
#将文件重命名
mv $filename $new
echo "The file $filename renamed to $new"

done

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

码字不易,点赞亲。

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                            <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;1582594662_002&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/xie_qi_chao">
                <img src="https://profile.csdnimg.cn/B/F/6/3_xie_qi_chao" class="avatar_pic" username="xie_qi_chao">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/2.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/xie_qi_chao" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">解启超</a></span>
                                        </div>
                <div class="text"><span>发布了357 篇原创文章</span> · <span>获赞 54</span> · <span>访问量 3万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=xie_qi_chao" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm attented bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">已关注</a>
                                </div>
                        </div>
                </div>
</article>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

尹汇川

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值