当前位置: 首页 > news >正文

linux shell 入门学习笔记16 流程控制开发

shell的流程控制一般包括if、for、while、case/esac、until、break、continue语句构成。

if语句开发

单分支if
//方式1
if <条件表达式>
then
代码。。。
fi
//方式2
if <条件表达式>;then
代码。。。
fi

双分支if
if <条件表达式>
then
代码1
if <条件表达式>
then
代码2
fi
fi

if-else处理
if <条件表达式>
then
当条件成立,会执行我
else
否则就会执行我
fi
// if-else 多分支处理
if <条件表达式1>
then
代码1
elif <条件表达式2>
then
代码2
elif <条件表达式3>
then
代码3
else
代码4
fi

if 实战
将之前的[] [[]] test条件判断的语句改成if+条件判断的语句
xiao123@xiao123:~/Downloads/shscripts$ [ -f /etc/hosts ] && echo yes
yes
xiao123@xiao123:~/Downloads/shscripts$ [[ -f /etc/hosts ]] && echo yes
yes
xiao123@xiao123:~/Downloads/shscripts$ test -f /etc/hosts && echo yes
yes
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ bash ./if_1.sh
[] is ok
[[]] is ok
test is ok
xiao123@xiao123:~/Downloads/shscripts$ cat ./if_1.sh
#! /bin/bashif [ -f /etc/hosts ]thenecho "[] is ok"
fiif [[ -f /etc/hosts ]]; thenecho "[[]] is ok"
fiif test -f /etc/hosts; thenecho "test is ok"
fi
xiao123@xiao123:~/Downloads/shscripts$

开发系统监控脚本

开发shell脚本

  1. 检查Linux剩余可用内存,当可用内存小于100M,就发邮件给运维。
  2. 并且该脚本加入crontab,每3分钟检查一次内存。
xiao123@xiao123:~/Downloads/shscripts$ bash free_1.sh
Current memory is 1081
内存不足,抓紧维护服务器!
xiao123@xiao123:~/Downloads/shscripts$ cat free_1.sh
#! /bin/bashFreeMem=`free -m |awk 'NR==2 { print $NF }'`CHARS="Current memory is ${FreeMem}"if [ "${FreeMem}"  -lt 2100 ];thenecho ${CHARS}|tee /tmp/message.txt# mail -s "主题" 收件人 <# mail -s "`date +%F-%T` ${CHARS}" yc_urrr@163.com < /tmp/message.txtecho "内存不足,抓紧维护服务器!"
fi
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ crontab -e
crontab: installing new crontab
xiao123@xiao123:~/Downloads/shscripts$
xiao123@xiao123:~/Downloads/shscripts$ crontab -l
*/3 * * * * /bin/bash /home/xiao123/Downloads/shscripts/free_1.sh
xiao123@xiao123:~/Downloads/shscripts$

if实战开发

单分支实战

xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  1 2
yes, 1 less than 2
xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  2 2
yes, 2 equal 2
xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  3 2
yes, 3 greather than 2
xiao123@xiao123:~/Downloads/shscripts$ cat ./if_read.sh
#! /bin/basha=$1
b=$2if [ $a -lt $b ]; thenecho "yes, $a less than $b"
fiif [ $a -eq $b ]; thenecho "yes, $a equal $b"
fiif [ $a -gt $b ]; thenecho "yes, $a greather than $b"
fi
xiao123@xiao123:~/Downloads/shscripts$

多分支实战

xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  1 1
yes, 1 equal 1
xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  1 2
yes, 1 less than 2
xiao123@xiao123:~/Downloads/shscripts$ bash ./if_read.sh  2 1
yes, 2 greather than 1
xiao123@xiao123:~/Downloads/shscripts$ cat ./if_read.sh
#! /bin/basha=$1
b=$2if [ $a -lt $b ]; thenecho "yes, $a less than $b"
elif [ $a -eq $b ]; thenecho "yes, $a equal $b"
elif [ $a -gt $b ]; thenecho "yes, $a greather than $b"
fi
xiao123@xiao123:~/Downloads/shscripts$
http://www.lryc.cn/news/23271.html

相关文章:

  • 机器学习:基于朴素贝叶斯对花瓣花萼的宽度和长度分类预测
  • 给VivoBook扩容重装系统
  • vue 依赖注入使用教程
  • 【再临数据结构】Day1. 稀疏数组
  • 二十四、MongoDB 聚合运算( aggregate )
  • 【C++】6.模板初阶
  • Docker部署Airbyte
  • 2023王道考研数据结构笔记第一章绪论
  • 告别空指针让代码变优雅,Optional使用图文例子源码解读
  • 【C++】哈希——unordered系列容器|哈希冲突|闭散列|开散列
  • mysql-面试
  • 【夏虫语冰】Win10局域网下两台电脑无法ping通: 无法访问目标主机
  • 大数据框架之Hadoop:MapReduce(三)MapReduce框架原理——Join多种应用
  • SSRF漏洞原理、危害以及防御与修复
  • CV学习笔记-ResNet
  • 百亿数据,毫秒级返回查询优化
  • cpp之STL
  • 基于Spring Boot开发的资产管理系统
  • Markdown总结
  • 字节跳动软件测试岗4轮面经(已拿34K+ offer)...
  • docker - 搭建redis集群和Etcd
  • Java程序开发中如何使用lntelliJ IDEA?
  • 【Linux】理解进程地址空间
  • Unity脚本 --- 常用API(类)--- GameObject类 和
  • HTML标签——表格标签
  • Telerik JustMock 2023 R1 Crack
  • 筑基八层 —— 问题思考分析并解决
  • 【面试题】当面试官问 Vue2与Vue3的区别,你该怎么回答?
  • 使用Python对excel中的数据进行处理
  • TCP协议原理三