shell训练计划30天之第十七天

例26 监控mysql服务

假设,当前mysql服务的root密码为123456,写脚本检测mysql服务是否正常(比如,可以正常进入mysql执行show processlist),并检测一下当前的mysql服务是主还是从,如果是从,请判断它的主从服务是否异常。如果是主,则不需要做什么

要点:

mysql -uroot -p123456 -e" show processlist"

show slave status

 

#!/bin/bash

mysql="usr/local/mysql/bin/mysql -uroot -p123456"

if ! $mysql "show processlist" >/dev/null 2>/dev/null

then

echo "Mysql serveric is down."

exit

else

$mysql -e "show slave status\G" 2>/dev/null >/tmp/slave.stat

n=`wc -l /tmp/slave.stat | awk '{print $1}'`

if [ $n -eq 0 ]

then

echo "This is master"

else

echo "This is slave"

egrep 'Slave_IO_Running:|Slave_SQL_Running:'|awk -F ': ' '{print $2}' > /tmp/SQL.tmp

if grep -qw "NO" /tmp/SQL.tmp

then

echo "The slave is down."

fi

fi

fi

 

 

例27 曾删用户

要求:写一个支持选项的增加或删除用户的shell脚本,具体要求如下

1.只支持三个选项'--del','--add','--help',输入其他选项报错

2.使用'--add'时,需要验证用户名是否存在,存在则反馈存在,且不添加。不存在则创建该用户,需要设置与该用户名相同的密码。

3.在使用'--del'时,需要验证用户名是否存在,存在则删除用户以及家目录。不存在则反馈该用户不存在。

4.--help选项反馈出使用方法。

5.能用echo $?检测脚本执行情况,成功删除或添加用户为0,不成功为非0整数

6.能以英文逗号分隔,一次性添加或者删除多个用户。例如 adddel.sh --add user1,user2,user3

核心

case判断

批量添加多用户,需要for循环。

 

 

 

#!/bin/bash

if [ $# -eq 0 ] || [ $# -gt 2 ]

then

echo "wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help."

exit

fi

ex_user()

{

if ! id $1 2>/dev/null >/dev/null

then

echo $1 not exsit.

useradd $1 && echo "$1 add successful."

else

echo $1 not exsit.

fi

}

notex_user()

{

if id $1 2>/dev/null >/dev/null

then

userdel $1 && echo "$1 delete successful"

 

else

echo $1 exist.

 

fi

}

case $1 in

--add)

if [ $# -eq 1 ]

then

echo "worng,use bash $0 --add user or bash $0 --add user1,user2,user3 ..."

exit

else

n=`echo $2|awk -F ',' '{print NF}'`

if [ $n gt 1 ]

then

for i in `seq 1 $n`

do

username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`

ex_user $username

done

else

ex_user $2

fi

fi

;;

--del)

if [ $# -eq 1 ]

then

echo "worng,use bash $0 --del user or bash $0 --del user1,user2,user3 ..."

exit

else

n=`echo $2|awk -F ',' '{print NF}'`

if [ $n gt 1 ]

then

for i in `seq 1 $n`

do

username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`

noex_user $username

done

else

noex_user $2

fi

fi

;;

 

 

--help)

if [ $# -ne 1 ]

then

echo "worng,use bash $0 --help"

exit

else

echo "Use bash $0 --add username or bash $0 --add user1,user2,user3 ... add user"

echo " bash $0 --del username r bash $0 --del user1,user2.user3 ... delete user"

echo " bash $0 --help print this info."

fi

;;

--del)

if [ $# -eq 1 ]

then

echo "worng,use bash $0 --del user or bash $0 --del user1,user2,user3 ..."

exit

else

n=`echo $2|awk -F ',' '{print NF}'`

if [ $n gt 1 ]

then

for i in `seq 1 $n`

do

username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`

noex_user $username

done

else

noex_user $2

fi

fi

;;

 

 

--help)

if [ $# -ne 1 ]

then

echo "worng,use bash $0 --help"

exit

else

echo "Use bash $0 --add username or bash $0 --add user1,user2,user3 ... add user"

echo " bash $0 --del username r bash $0 --del user1,user2.user3 ... delete user"

echo " bash $0 --help print this info."

fi

;;

--del)

if [ $# -eq 1 ]

then

echo "worng,use bash $0 --del user or bash $0 --del user1,user2,user3 ..."

exit

else

n=`echo $2|awk -F ',' '{print NF}'`

if [ $n gt 1 ]

then

for i in `seq 1 $n`

do

username=`echo $2 |awk -v j=$i -F ',' '{print $j}'`

noex_user $username

done

else

noex_user $2

fi

fi

;;

 

 

--help)

if [ $# -ne 1 ]

then

echo "worng,use bash $0 --help"

exit

else

echo "Use bash $0 --add username or bash $0 --add user1,user2,user3 ... add user"

echo " bash $0 --del username r bash $0 --del user1,user2.user3 ... delete user"

echo " bash $0 --help print this info."

fi

;;

*)

echo "worng,use bash $0 --add user or bash $0 --add user1,user2,user3 ..."

;;

esac

 

 

例28 计算和

要求:写一个脚本:计算100以内所有能被3整除的正整数的和

要点:被三整除,余数为0

for循环 求和

 

 

例29 加减乘除

要求:使用传参的方法写个脚本,实现加减乘除的功能

例如:sh a.sh 1 2 ,这样会分别计算加减乘除的结果

要求:

脚本需判断提供的两个数字必须为整数

当做减法或者除法时,需要判断那个数字大,减法时需要用大的数字减小的数字,除法时用大的数字减除以小的数字,并且结果或需要保留两个小数点

核心要点

参数只能是两个整数

判断数字大小

 

 

 

#!/bin/bash

is_nu()

{

n=`echo $1` | sed 's/[0-9]//g'

if [ -n "$n" ]

then

echo "请输入正整数"

exit

fi

 

}

if [ $# -ne 2 ]

then

echo "必须要输入两个参数"

exit

else

is_nu $1

is_nu $2

fi

 

big()

{

if [ $1 -gt $2 ]

then

echo $1

else

echo $2

fi

}

small()

{

if [ $1 -lt $2 ]

then

echo $1

else

echo $2

fi

}

 

add()

{

sum=$[$1+$2]

echo "$1+$2=$sum"

}

 

jian()

{

b=`big $1 $2`

s=`small $1 $2`

cha=$[$b-$s]

echo "$b-$s=$cha"

}

 

cheng()

{

ji=$[$1*$2]

echo "$1x$2=$ji"

 

}

chu()

{

b=`big $1 $2`

s=`small $1 $2`

v=`echo "scale=2;$b/$s"|bc`

echo "$b/$s=$v"

}

add $1 $2

jian $1 $2

cheng $1 $2

chu $1 $2

 

 

例子30 输入数字

要求:写一个脚本,执行后打印please input a number:,要求用户输入数值,然后打印出该数值,然后再次要求该用户输入数值。直到用书输入“end”停止

要点

判断用户输入的字符是否是纯数字

 

 

 

#!/bin/bash

while :

do

read -p "Please Input a number:" n

if [ -z "$n" ]

then

echo "请输入一个纯数字"

continue

fi

if echo $n | grep -qi 'end'

then

exit

fi

n1=`echo $n | sed 's/[0-9]//g'`

if [ -n "$n1" ]

then

echo "请输入一个纯数字。"

continue

else

echo "你输入的数字是:$n"

continue

fi

done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值