运维Shell脚本小试牛刀(五):until循环|循环控制break|continue


运维Shell脚本小试牛刀(一)

运维Shell脚本小试牛刀(二)

运维Shell脚本小试牛刀(三)::$(cd $(dirname $0); pwd)命令详解

运维Shell脚本小试牛刀(四): 多层嵌套if...elif...elif....else fi_蜗牛杨哥的博客-CSDN博客

Cenos7安装小火车程序动画

运维Shell脚本小试牛刀(五):until循环

运维Shell脚本小试牛刀(六): Shell中的函数认知




  一: until与while类似,实现循环

 until基于一个条件表达式,添加不满足时执行until循环体, 但until与while条件判断相反,until在条件为假的时候才执行,一旦添加为真, 则退出循环体.

[root@www dicfor]# cat untilloop.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  untilloop.sh
#                           USAGE: ./untilloop.sh
#    DESCRIPTION: until与while类似,但是until时但条件不为真才执行
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
var=1

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

   var=$(( var + 1 ))

done
 

二:测试脚本 


[root@www dicfor]# vi untilloop.sh 
[root@www dicfor]# ./untilloop.sh 
The for loop is run 1 times.................
The for loop is run 2 times.................
The for loop is run 3 times.................
[root@www dicfor]# vi untilloop.sh 
 


三: break循环控制

break与continue使用bash中循环控制的命令,break语句用于从for,while,until或者select循环体重退出;其语法为

  break [n]

n代表嵌套循环的层级,如果指定了n,break将退出n级嵌套循环;如果没有指定n或者n不大于或者等于1,则退出状态码为0,否则退出状态码为n;

#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  forbreak.sh
#                           USAGE: ./forbreak.sh
#    DESCRIPTION:
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
# 如果为指定参数,则打印脚本的使用方法,并返回退出状态码1

[ $# -eq 0 ] && { echo "Usage: $0 filepath"; exit 1; }

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

# 遍历目录/etc/* 下的所有文件

for file in /etc/*
do
 # 如果文件的路径与指定的参数文件路径匹配,则打印文件已找到,并退出for循环
if [ $file == "$match" ]
   then
   
    echo "The file $match was found........"
   cat $match 
   found=1
   #使用break退出for循环
   break
   fi
done

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

四: 测试break样例脚本


[root@www dicfor]# ./forbreak.sh /etc/inittab
The file /etc/inittab was found........
./forbreak.sh:行41: found: 未找到命令
The file /etc/inittab not found in /etc directory..
[root@www dicfor]# vi forbreak.sh 
[root@www dicfor]# ./forbreak.sh 
Usage: ./forbreak.sh filepath
[root@www dicfor]# ./forbreak.sh /etc/inittab
The file /etc/inittab was found........
[root@www dicfor]# ./forbreak.sh /etc/hosts
The file /etc/hosts was found........
[root@www dicfor]# ./forbreak.sh /etc/my.cnf 
The file /etc/my.cnf was found........
[root@www dicfor]# vi forbreak.sh 
[root@www dicfor]# ./forbreak.sh /etc/my.cnf 
The file /etc/my.cnf was found........
[mysql]
# 设置mysql客户端默认字符集
default-character-set=utf8mb4
[client]
# 设置mysql客户端连接服务端时默认使用的端口
port=3306
default-character-set=utf8mb4
[mysqld] 
#character config
character_set_server=utf8mb4
#设置端口
port=3306
explicit_defaults_for_timestamp=true
# 允许最大连接数
max_connections=1000
#设置mysql安装目录
datadir=/usr/local/mysql8/mysql-8.0.30
# 设置mysql数据库的数据的存放目录
datadir=/usr/local/mysql8/mysql-8.0.30/data
# 允许连接失败的次数。这是为了防止有人从该主机试图攻击数据库系统
max_connect_errors=100

# 每次commit 日志缓存中的数据刷到磁盘中。通常设置为 1,意味着在事务提交前日志已被写入磁盘, 事务可以运行更长以及服务崩溃后的修复能力
innodb_flush_log_at_trx_commit=1
sync_binlog=1
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
# 是否对sql语句大小写敏,1表示不敏感 (注意:这个在mysql8中已经无法在配置文件中修改了,只有在初始化的时候进行配置,且如果初始化时配置了该属性,则在本配置文件中必须要有该属性的设置,否则启动报错。意思就是要么两个地方都配置,要么这里也不要配置。不然迎接你的只有报错)
#lower_case_table_names = 1
#MySQL连接闲置超过一定时间后(单位:秒)将会被强行关闭
#MySQL默认的wait_timeout  值为8个小时, interactive_timeout参数需要同时配置才能生效
interactive_timeout = 1800
wait_timeout = 1800
#Metadata Lock最大时长(秒), 一般用于控制 alter操作的最大时长sine mysql5.6
#执行 DML操作时除了增加innodb事务锁外还增加Metadata Lock,其他alter(DDL)session将阻塞
lock_wait_timeout = 3600
#内部内存临时表的最大值。
#比如大数据量的group by ,order by时可能用到临时表,
#超过了这个值将写入磁盘,避免系统IO压力增大,默认16M
tmp_table_size = 64M
max_heap_table_size = 64M

# 配置密码插件使用低版本
default_authentication_plugin=mysql_native_password
# pid-file文件配置
pid-file=/usr/local/mysql8/mysql-8.0.30/mysqld.pid
# 配置日志目录
log_error=/usr/local/mysql8/mysql-8.0.30/log/mysql-error.log
slow_query_log_file=/usr/local/mysql8/mysql-8.0.30/log/mysql-slow.log
# 设置SQL模式
sql_mode="STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION"
#不限制导入导出 任意目录/temp/data/
secure_file_priv='/temp/data'

# 配置服务ID
server-id=100
# 配置二进制日志目录
log-bin=/usr/local/mysql8/mysql-8.0.30/binlog
#主从复制忽略的数据库
binlog-ignore-db=mysql
binlog-ignore-db=information_schema
#开启主从复制的数据库
binlog-do-db=tope-pay-user
# bin log 日志格式
#STATEMENT:记录主库执行的SQL复制到从库; 调用时间函数时会导致主从数据不一致
#ROW:记录主库每一行的变化;效率低
#MIXED:修复一些主从数据不一致情况;本地变量调用还会存在问题;@@hostname
binlog_format=statement
#二进制日志自动删除/过期的天数。默认值为0,表示不自动删除
expire_logs_days=7
#跳过主从复制中遇到的所有错误或指定类型的错误
slave_skip_errors=1062
 

五: break跳出for循环多层嵌套 


[root@www dicfor]# cat breaknestedloop.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#                           FILE:  breaknestedloop.sh
#                           USAGE: ./breaknestedloop.sh
#    DESCRIPTION:   break n 循环嵌套脚本
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-24 09:11:20    
#        REVISION: --------
#
#
#
#
#
#====================================================================================================================
#如果未传递参数,则打印脚本的使用方法,并返回退出状态码1

[ $# -eq 0 ] && echo "Usage: $0 command......."

# 将位置参数的值赋值给match变量,

match=$1
found=0

for dir in /bin /usr/bin
do
  echo "当前变量的目录为: ${dir}"
  #遍历目录下的所有的文件
  for file in $dir/*
  do
  echo "${dir} 目录中的文件为: $file "
# 如果文件名与指定的参数文件名匹配,则打印命令与找到,并退出嵌套的for循环
  if [ $(basename $file ) == "$match" ]
  then
    echo "遍历的目录中匹配到的文件为: $file"
    echo "The command $match was found..................."
    found=1
    # 退出两层的for循环
    break 2
   fi
 done

done
[ $found -ne 1 ] && echo "The command $match not found................"

六: 测试输出 


[root@www dicfor]# ./breaknestedloop.sh /usr/bin/yes
当前变量的目录为: /bin
/bin 目录中的文件为: /bin/[ 
/bin 目录中的文件为: /bin/a2p 
/bin 目录中的文件为: /bin/addr2line 
/bin 目录中的文件为: /bin/alias 
/bin 目录中的文件为: /bin/amuFormat.sh 
/bin 目录中的文件为: /bin/apropos 
/bin 目录中的文件为: /bin/ar 
/bin 目录中的文件为: /bin/arch 
/bin 目录中的文件为: /bin/as 
/bin 目录中的文件为: /bin/aserver 
/bin 目录中的文件为: /bin/attr 
/bin 目录中的文件为: /bin/aulast 
/bin 目录中的文件为: /bin/aulastlog 
/bin 目录中的文件为: /bin/ausyscall 
/bin 目录中的文件为: /bin/autoconf 
/bin 目录中的文件为: /bin/autoheader 
/bin 目录中的文件为: /bin/autom4te 
/bin 目录中的文件为: /bin/autoreconf 
 

七: cowsay 命令安装输出图案

[root@www tools]# yum -y install cowsay   
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.ustc.edu.cn
 * extras: mirrors.ustc.edu.cn
 * updates: mirrors.ustc.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 cowsay.noarch.0.3.04-4.el7 将被 安装
--> 解决依赖关系完成


[root@www tools]# animalsay ‘1’
 _____
< ‘1’ >
 -----
  \
   \   \_\_    _/_/
    \      \__/
           (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||
 

八:continue循环控制 


[root@www dicfor]# cat continueloop.sh 
#!/bin/bash -
#==================================================================================================================
#
#
#    FILE:  checknumber.sh
#    USAGE: ./checknumber.sh
#
#         
#    DESCRIPTION:  continue循环控
#        OPTIONS: -------
#        REQUIREMENTS: ---------

#        BUGS: ------
#        NOTES: -------- 
#        AUTHOR: ---------YangGe (TOM) ,YangGe.freedom@icloud.com
#    ORGANIZATION:
#        CREATED: 2023-8-29 09:11:20
#        REVISION: --------
#
#
#
#
#
#===================================================================================================================
# 如果未指定运行脚本的指定参数,则打印脚本的使用方法,并返回退出状态码1
[ $# -eq 0 ] && { echo "Usage: $0 directory"; exit 1; }

# 如果指定的目录不存在,则打印错误信息,并返回退出状态码1

[ ! -d $1 ] && { echo "Error: The directory $1 does not exist."; exit 1; }

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

# 遍历指定目录下的所有文件
for filename in $(ls)

do
    # 如果文件名不包含大写字母,则直接跳转到下一次循环
    if [ $filename != *[[:upper:]]* ]
    then
    echo "not exists satisfy files !"
    # 忽略for循环体中剩余的语句直接跳转到下一次循环
    continue
    fi
    
    # 将变量中的字母转换为小写

    new=`echo $filename | tr 'A-Z' 'a-z'`
    # 将文件重新命名
    mv $filename $new
    echo "The file $filename renamed to $new."
done
 


脚本运行结果:


[root@www dicfor]# ./continueloop.sh 
Usage: ./continueloop.sh directory
[root@www dicfor]# ./continueloop.sh /usr/local/example/dicfor/logs
[root@www dicfor]# ./continueloop.sh /usr/local/example/dicfor/log
Error: The directory /usr/local/example/dicfor/log does not exist.
[root@www dicfor]# ./continueloop.sh /usr/local/example/dicfor/log
Error: The directory /usr/local/example/dicfor/log does not exist.
[root@www dicfor]# ./continueloop.sh /usr/local/example/dicfor/logs
not exists satisfy files !
[root@www dicfor]# mkdir -p ./logs/TEST.TXT
[root@www dicfor]# ./continueloop.sh /usr/local/example/dicfor/logs
not exists satisfy files !
The file TEST.TXT renamed to test.txt.
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值