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

文章介绍了Shell脚本中的流程控制结构,包括if、if-else和if-elif-else的使用方法,并提供了实际示例,如检查文件存在性和内存监控。当系统内存低于特定值时,脚本会发送警告邮件。此外,还展示了如何通过crontab设置定时任务进行定期检查。
摘要由CSDN通过智能技术生成

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/bash

if [ -f /etc/hosts ]
        then
                echo "[] is ok"
fi

if [[ -f /etc/hosts ]]; then
        echo "[[]] is ok"
fi

if test -f /etc/hosts; then
        echo "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/bash

FreeMem=`free -m |awk 'NR==2 { print $NF }'`

CHARS="Current memory is ${FreeMem}"

if [ "${FreeMem}"  -lt 2100 ];
   then
        echo ${CHARS}|tee /tmp/message.txt
        # mail -s "主题" 收件人 <
        # mail -s "`date +%F-%T` ${CHARS}" yc_urrr@163.com < /tmp/message.txt
        echo "内存不足,抓紧维护服务器!"
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/bash

a=$1
b=$2

if [ $a -lt $b ]; then
    echo "yes, $a less than $b"
fi

if [ $a -eq $b ]; then
    echo "yes, $a equal $b"
fi

if [ $a -gt $b ]; then
    echo "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/bash

a=$1
b=$2

if [ $a -lt $b ]; then
    echo "yes, $a less than $b"
elif [ $a -eq $b ]; then
    echo "yes, $a equal $b"
elif [ $a -gt $b ]; then
    echo "yes, $a greather than $b"
fi
xiao123@xiao123:~/Downloads/shscripts$
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值