书写一些简单的shell脚本

1、检查定时任务是否开启

[root@shell 2.5]# cat sb01.sh 
#!/bin/bash
. /etc/init.d/functions

service=`ps -ef | grep crond | wc -l`

if [ $service -gt 1 ];then
	echo `action "crond is running" /bin/true`
else
	echo `action "crond is failed" /bin/false`
fi

2、检查内存是否充足

[root@shell 2.5]# cat sb02.sh 
#!/bin/env bash

n=`free | awk -F ' ' 'NR==2 {print $7} '`

if [ $n -gt 102400 ];then
	echo "内存充足"
	exit
else
	echo "内存不足"
	exit
fi

3、检查Web网站URL是否正常运行

[root@shell 2.5]# cat sb03.sh 
#!/bin/bash
source /etc/init.d/functions

read -p '请输入测试域名:' str1

error () {
if [ $# -ne 1 ];then
	echo "只能输入一个域名"
        exit
fi
}

error $str1

if ! ping -w3 -c1 $str1 &>/dev/null;then
        echo "该网站不存在或网站格式书写错误"
        exit
fi

wget --tries=1 -T 5 $strl &>/dev/null

if [ $? -eq 0 ];then
        echo "正在连接$str1...."
        sleep 1
        action "$1连接正常" /bin/true
else
        echo "正在连接$str1...."
        sleep 1
        action "$str1无法正常通讯" /bin/false
fi

4、rsync服务管理脚本

[root@shell 2.5]# cat sb04.sh 
#!/bin/bash
source /etc/init.d/functions

pid_num=/var/run/rsyncd.pid
choice=$1

start() {
if [ -s "$pid_num" ]
then
	echo "running"
else
	rsync --daemon
fi
}

stop() {
if [ -s "$pid_num" ]
then
	kill `cat $pid_num`
else
	echo "stopped"
fi
} 

restart() {
	stop
	sleep 2
	start
}

case "$choice" in 
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	*)
		echo -e "\033[31mUsages: $0 {start|stop|restart} \033[0m"
		exit 1
esac

5、生成数列脚本

[root@shell 2.5]# cat sb05.sh 
#!/bin/bash
source /etc/init.d/functions

num=1
while [ $num -le 10 ]
do
	echo $num
	let num++
done

6、一到一百累加运算

[root@shell 2.5]# cat sb06.sh 
#!/bin/bash
source /etc/init.d/functions

num=1
sum=0
while [ $num -le 100 ]
do
	#echo $num
	((sum=num+sum))
	(( num++ ))
	echo $sum 依次累加结果
done
echo $sum 最终结果

7、随机生成十个文件(因为生成名字会出现重复字母很有可能生成不够)

[root@shell 2.5]# cat lj01.sh 
#!/bin/bash

[ -d /test ] || mkdir -p /test 

a=`date +%N | md5sum | head -c10 | xargs echo | sed -nr "s#(.)#\1 #gp"`
b=$a

eval echo $b
for i in $b
do
	touch /test/$i.html
done

8、随机生成十个文件

[root@shell 2.5]# cat lj02.sh 
#!/usr/bin/env bash

o=/good
[ -d $o ] || mkdir -p /good

for i in {1..10}
do
	test=`date +%N | md5sum | head -c10 | xargs echo`
	touch $o/$test\_.html
done

9、恶搞脚本,别喷我

[root@shell 2.5]# cat good3.sh 
#!/bin/bash
trap "" HUP INT TSTP

while true
do
read -p "你说谁是猪:"  goodnb
case $goodnb in
        "我是猪")
                echo "对你的确是猪"
                sleep 100
                exit
                ;;
        *)
                echo "呸,你居然说自己不是猪"
esac
done
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值