shell脚本练习

一、写一个脚本,完成如下功能 传递一个参数给脚本,此参数为gzip、bzip2或者xz三者之一;

(1) 如果参数1的值为gzip,则使用tar和gzip归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.gz;

(2) 如果参数1的值为bzip2,则使用tar和bzip2归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.bz2;

(3) 如果参数1的值为xz,则使用tar和xz归档压缩/etc目录至/backups目录中,并命名为/backups/etc-20160613.tar.xz;

(4) 其它任意值,则显示错误压缩工具,并执行非正常退出;

[root@localhost scripts]# mkdir /scripts/backups
[root@localhost scripts]# vim compress_work.sh
[root@localhost scripts]# bash compress_work.sh
[root@localhost scripts]# ll /backups/

二、报警脚本

要求如下:

根分区剩余空间小于20%

发送告警邮件给自己

配合crond每5分钟检查一次脚本

#根剩余内存
[root@localhost scripts]# df | grep /dev/mapper/openeuler-root | tr -s " " | cut -d " " -f 4
24483368
#根总共内存
[root@localhost scripts]# df  | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 2
27687012
#占比
[root@localhost scripts]# num=` echo $mem*100 / $total | bc`
[root@localhost scripts]# echo $num
88


#shell脚本编辑
[root@localhost scripts]# vim work.sh
#!/bin/bash
#根剩余内存
a=`df  | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 4`
#根总共内存
total=`df  | grep /dev/mapper/openeuler-root | tr -s " " " " | cut -d " " -f 2`
num=` echo $mem*100 / $total | bc`
if [ $num -lt 20 ]
then
        echo 内存警告 | mail -s "警告" 3126929762@qq.com
fi


#定时
[root@localhost ~]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
5 * * * * bash /scripts/work.sh
[root@localhost ~]# crontab -l
5 * * * * bash /scripts/work.sh

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值