博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Shell中的循环判断语句(2)while语句、until语句
阅读量:3936 次
发布时间:2019-05-23

本文共 2559 字,大约阅读时间需要 8 分钟。

while语句

1.特点:条件为真就进入循环;条件为假就退出循环

2.语法结构:

while   表达式    do          command...    done

入门案例:

计算1-50偶数和

在这里插入图片描述
在这里插入图片描述
until语句
1.特点 : 和 while 刚好相反,只要不满足条件就一直循环 ( 屡败屡战 )
2.语句结构:

until expression     do          command    done

入门案例:

计算1-50偶数和

在这里插入图片描述

在这里插入图片描述
实战脚本
练习1:编写一脚本,30秒同步一次系统时间。若同步失败,则进行邮件报警;若同步成功,每成功一百次发一封邮件进行通知。
在这里插入图片描述
练习2:写一个脚本自动搭建nfs服务

#!/bin/bashecho -e  "\033[32m######testing the network.......\033[0m"ping -c1 -w1 172.25.254.6 &>/dev/null	if [ $? -eq 0 ]	then		echo  -e "\033[32mthe network is ok !!\033[0m"	else		echo  -e "\033[31mthe network is not running!!\033[0m"		exit	fiecho -e "\033[32m########turnning down the selinux.......\033[0m"setenforce 0 &>/dev/null	if [$? -eq 0 ]	then		echo  -e "\033[32mselinux is turning down\033[0m"	fisystemctl stop firewalld &>/dev/null	if [ $? -eq 0 ]	then		echo -e "\033[32mfirewall is stoped"	fiecho -e "\033[32m########testing the software......\033[0m"rpm -q rpcbind &>/dev/null	if [ $? -eq 0 ]	then		echo "rpcbind is already installed"     	else		echo "rpcbind is not installed"dnf install rpcbind -y        if [ $? -eq 0 ];then                         echo "rpcbind is now installed"        else                         echo "rpcbind install failed"                            exit                  fi             exit	fiecho -e "\033[32m#######mkdir dir network chmod .......\033[0m"read -p "Please input share dir:  " dir[ -e $dir ] && {        echo "the dir exists"}||{        mkdir -p $dir        echo "$dir create success"        exit}chmod 1777 $dirread -p "please input the subnet to share: " subnetread -p "please input share permission: " permission echo -e "\033[32medit nfs configure file /etc/exports\033[0m"read -p "input 1 -> clear config,default is add: " choiceif [ $choice -eq 1 ];then     > /etc/exportsficat >> /etc/exports << EOF$dir $subnet($permission)EOF echo -e "\033[32msetting service start with poweron and start searvice......\033[0m " echo -e "\033[31mcheck nfs-server is start?\033[0m"systemctl status nfs-server.service | grep active &>/dev/nullif [ $? -eq 0 ];then        systemctl restart nfs-server        echo "Nfs server restart success"else        systemctl start rpcbind        systemctl start nfs-server        systemctl enable --now rpcbind        systemctl enable --now nfs-serverfi  echo -e "\033[32m########testing wheather network could be use......."showmount -e 172.25.254.6 | grep $dir &> /dev/null[ $? -eq 0 ] && {        echo -e "\033[32mshare success\033[0m" }||{        echo -e "\033[32mshare failed\033[0m"        exit }echo -e "\033[32mnfs.service is created successfully!!!\033[0m"

转载地址:http://voegn.baihongyu.com/

你可能感兴趣的文章
mysql,命令导入\导出表结构或数据
查看>>
MySQL使用Navicat导出数据,数据会被减少
查看>>
java中NIO对文件的读取操作
查看>>
idea中@Data标签getset不起作用
查看>>
git使用
查看>>
spring boot jpa多表查询展示实体的方法
查看>>
javanio中FileChannel写入文件write,追加文件,以及多文件合并
查看>>
jpa操作mysql乱码以及中文查询错误
查看>>
必须串行执行程序如何提高执行效率之队列方法
查看>>
@Cacheable在同一个类中方法调用不起作用
查看>>
Vysor:在电脑里控制你的安卓手机
查看>>
带时区的时间MySQL以及java中的处理
查看>>
boot中jar包部署的方式读取classes下的文件
查看>>
微服务架构的基础框架选择:Spring Cloud还是Dubbo?
查看>>
mapdb实现分析
查看>>
Spring Cloud Netflix Eureka源码导读与原理分析
查看>>
gemfire的简单了解
查看>>
pom配置之:<distributionManagement>snapshot快照库和release发布库
查看>>
Hystrix仪表盘--Unable to connect to Command Metric Stream
查看>>
文本入库特殊字符处理, 防止SQL注入
查看>>