1、假如当前mysql密码为 nihao123! 写脚本检测mysql服务是否正常(比如 可以进入mysql 里 执行 show processlist),并检测当前mysql是主还是从,如果是从,则判断 它 的主是否正常,如果是主,则什么也不做;
[root@localhost_002 shell100]# cat 22.sh
#!/bin/bash
mysql="/usr/local/mysql/bin/mysql -uroot -pnihao123!"
if ! $mysql -e "show processlist" > /dev/null 2>&1
then
echo "msyql service is down;"
exit
else
$mysql -e "show slave status\G" >/tmp/slave.state 2>/dev/null
n=`wc -l /tmp/slave.state`
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
if grep -qw "No" /tmp/tmp.sql
then
echo "The is salve is down"
fi
fi
fi
注意分三步:首先通过是否可以执行show processlist 来判断mysql服务是否可用;如下:
[root@localhost_002 shell100]# mysql -uroot -pnihao123! -e "show processlist"
Warning: Using a password on the command line interface can be insecure.
+----+------+-----------+------+---------+------+-------+------------------+
| Id | User | Host | db | Command | Time | State | Info |
+----+------+-----------+------+---------+------+-------+------------------+
| 3 | root | localhost | NULL | Query | 0 | init | show processlist |
+----+------+-----------+------+---------+------+-------+------------------+
注释: mysql -e 选项可以用来执行命令; 用法: mysql -uroot -pnihao123! -e "show processlist"
然后用 if 来作为判断条件了; if ! $mysql -e "show processlist" 如果没正常执行,则怎么样;
然后再判断mysql主从,通过打印 show slave status\G内容写入到一个文件,然后判断这个文件的行数是否等于 0 ,如果是主,则没有任何内容 | 也就是等于 0 , 否则就是从; 然后在通过show slave status\G,通过egrep 过滤出来 Slave_IO_Running:和Slave_SQL_Runnning:打印出第二行,判断是否等于 No ,如果是,则主从已经挂了,可以设置发邮件或告警等操作;
2、写一个支持选项的增加或删除用户的shell脚本,具体要求如下:
只支持三个选项:'--del','--add','--help',输入其他选项报错。
使用'--add'时,需要验证用户名是否存在,存在则反馈存在,且不添加。 不存在则创建该用户,需要设置与该用户名相同的密码。
使用'--del'时,需要验证用户名是否存在,存在则删除用户及其家目录。不存在则反馈该用户不存在。
--help选项反馈出使用方法。
能用echo $?检测脚本执行情况,成功删除或添加用户为0,不成功为非0正整数。
能以英文逗号分割,一次性添加或者删除多个用户。例如 adddel.sh --add user1,user2,user3
#!/bin/baash
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
useradd $1 && echo "$1 add successful."
else
echo $1 exist.
fi
}
notex_user()
{
if id $1 2>/dev/null >/dev/null
then
userdel $1 && echo "$1 delete successful."
else
echo $1 not exist.
fi
}
case $1 in
--add)
if [ $# -eq 1 ]
then
echo "Wrong, 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 "Wrong, 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}'`
notex_user $username
done
else
notex_user $2
fi
fi
;;
--help)
if [ $# -ne 1 ]
then
echo "Wrong, 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 "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
;;
esac
3、写一个脚本,计算100以所有被3整除的正整数值得和;
分析:能被 3 整除,那就是这个数除以 3 ,余数是 0 ,在shell中的表达式为:$[$i%3] i是变量名
所有数字求和:设置一个初始值为 0 ,每循环一次,加一个数字得到新的和;
注释: for i in `seq 1 100` 等同于 for i in {1..100}
[root@localhost_002 shell100]# cat 28.sh
#!/bin/bash
sum=0
for i in {1..100}
do
n=$[$i%3]
if [ $n -eq 0 ]
then
sum=$[$i+$sum]
fi
done
echo "$sum"
[root@localhost_002 shell100]# sh 28.sh
1683
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 才退出;
满足特定的条件( end )才退出,适合用户 while;
提示用户输入值: 用 read -p
然后用户输入的值用 if 做一个判断 是否等于 end;
[root@localhost_002 shell100]# cat 30.sh
#!/bin/bash
while :
do
read -p "please input number: " n
num=`echo $n|sed 's#[0-9]##g'|wc -L`
if [ $n == "end" ]
then
echo "bye"
exit
fi
if [ $num -ne 0 ]
then
echo "you input is not number, try again"
else
echo "you input number is:$n"
fi
done
[root@localhost_002 shell100]# sh 30.sh
please input number: abc
you input is not number, try again
please input number: 456
you input number is:456
please input number: en
you input is not number, try again
please input number: end
bye
注释:`sed 's#[0-9]##g'|wc -L` 会把所有的数字删除,只留下非数字的字符;wc -L 是统计最长行的字符串长度,长度为0,说明是已经被替换的为空的纯数字,否则是非数字字符,不符合要求;
注释:在shell 中,判断两个字符串是否相等,则需要用 ==,不能使用 -eq , -eq 是使用来判断数字,
如:a=abc;b=abc; if [ $a == $b ];then echo $a;fi
wc -l 统计 行数; list
wc -w 统计单词数; word
wc -m 统计字符数;
wc -L 统计最长行的长度;