笔试题 shell
Posted 乌托邦眺望
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了笔试题 shell相关的知识,希望对你有一定的参考价值。
企业实践题1:(生产实战案例):监控mysql主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟:
阶段1:开发一个守护进程脚本每30秒实现检测一次。
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)
mysql查看状态语句
mysql>show slave status\G
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 10.0.0.42
Master_User: backup
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000001
Read_Master_Log_Pos: 538
Relay_Log_File: mysql-relay-bin.000002
Relay_Log_Pos: 701
Relay_Master_Log_File: mysql-bin.000001
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
使用MysqlCmd2 命令筛选后语句:
Yes
Yes
0
0
it
#!/bin/bash port=3306 error=(1158 1159 1008 1007 1062) MysqlCmd1= mysql -uroot -p192.168.51.9 -e MysqlCmd2= mysql -uroot -p192.168.51.9 -e"show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk ‘{print $NF}‘ function is_run(){ [ `lsof -i:$port|wc -l` -lt 2 ]&&{ echo"mysql is stop" exit 1 } } function mysql_status(){ array=(`$MysqlCmd2`) } function judge_error(){ for i in ${error[*]} do if [ "${array[2]}" == "$i" ];then $MysqlCmd1"stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;start slave;" else echo "mysql is failed,error id is ${array[2]}" fi done } judge_status(){ mysql_status echo ${array[*]} if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[3]}" == "0" ];then echo "Mysql slave is ok" else judge_error ${array[2]} fi } function main(){ while true do is_run judge_status sleep 30 done } main
1 #!/bin/bash 2 3 port=3306 4 error=(1158 1159 1008 1007 1062) 5 MysqlCmd1= mysql -uroot -p192.168.51.9 -e 6 MysqlCmd2= mysql -uroot -p192.168.51.9 -e"show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk ‘{print $NF}‘ 7 8 function is_run(){ 9 [ `lsof -i:$port|wc -l` -lt 2 ]&&{ 10 echo"mysql is stop" 11 exit 1 12 } 13 } 14 15 16 function mysql_status(){ 17 array=(`$MysqlCmd2`) 18 } 19 20 21 function judge_error(){ 22 for i in ${error[*]} 23 do 24 if [ "${array[2]}" == "$i" ];then 25 $MysqlCmd1"stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;start slave;" 26 else 27 echo "mysql is failed,error id is ${array[2]}" 28 fi 29 done 30 } 31 32 judge_status(){ 33 mysql_status 34 echo ${array[*]} 35 if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[3]}" == "0" ];then 36 echo "Mysql slave is ok" 37 else 38 judge_error ${array[2]} 39 fi 40 } 41 42 43 function main(){ 44 while true 45 do 46 is_run 47 judge_status 48 sleep 30 49 done 50 } 51 52 53 main
#!/bin/bash port=3306 error=(1158 1159 1008 1007 1062) MysqlCmd1= mysql -uroot -p192.168.51.9 -e MysqlCmd2= mysql -uroot -p192.168.51.9 -e"show slave status\G"|egrep "_Running|Last_Errno|Behind_Master"|awk ‘{print $NF}‘ function is_run(){ [ `lsof -i:$port|wc -l` -lt 2 ]&&{ echo"mysql is stop" exit 1 } } function mysql_status(){ array=(`$MysqlCmd2`) } function judge_error(){ for i in ${error[*]} do if [ "${array[2]}" == "$i" ];then $MysqlCmd1"stop slave;SET GLOBAL SQL_SLAVE_SKIP_COUNTER = 1;start slave;" else echo "mysql is failed,error id is ${array[2]}" fi done } judge_status(){ mysql_status echo ${array[*]} if [ "${array[0]}" == "Yes" -a "${array[1]}" == "Yes" -a "${array[3]}" == "0" ];then echo "Mysql slave is ok" else judge_error ${array[2]} fi } function main(){ while true do is_run judge_status sleep 30 done } main
企业实践题2:
使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:
coaolvajcq_oldboy.html qnvuxvicni_oldboy.html
#!/bin/for((i=1;i<=10;i++ )) do a=`tr -dc "a-z" < /dev/urandom | head -c 10`_oldboy,html touch $dir/$a done
#tr -dc 的意思是,将字符串中a到z 以外的字符提取并删除
以上是关于笔试题 shell的主要内容,如果未能解决你的问题,请参考以下文章