shell习题-17
发布时间:2020-12-16 01:42:27 所属栏目:安全 来源:网络整理
导读:题目要求 假设,当前MySQL服务的root密码为123456,写脚本检测MySQL服务是否正常(比如,可以正常进入mysql执行show processlist), 并检测一下当前的MySQL服务是主还是从,如果是从,请判断它的主从服务是否异常。如果是主,则不需要做什么。 参考答案 #!/
题目要求
假设,当前MySQL服务的root密码为123456,写脚本检测MySQL服务是否正常(比如,可以正常进入mysql执行show processlist), 并检测一下当前的MySQL服务是主还是从,如果是从,请判断它的主从服务是否异常。如果是主,则不需要做什么。 参考答案#!/bin/bash mysql="/usr/local/mysql/bin/mysql -uroot -p123456" if ! $mysql -e "show processlist" >/dev/null 2>/dev/null then echo "MySQL service is down." exit else $mysql -e "show slave statusG" 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." #找出Slave_IO_Running和Slave_SQL_Running是yes 还是no egrep ‘Slave_IO_Running:|Slave_SQL_Running:‘/tmp/slave.stat|awk -F ‘: ‘ ‘{print $2}‘ > /tmp/SQL.tmp #如果为no则down掉了 if grep -qw "No" /tmp/SQL.tmp then echo "The slave is down." fi fi fi 题目要求写一个支持选项的增加或删除用户的shell脚本,具体要求如下:
参考答案#!/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 #useradd用户函数 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 ] //参数为1 直接报错退出 then echo "Wrong,use bash $0 --add user or bash $0 --add user1,user3..." exit else n=`echo $2| awk -F ‘,‘ ‘{print NF}‘` //用户个数 #用户个数大于1的,需要轮询下,创建,等于1的直接创建 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 ] //参数为1 直接报错退出 then echo "Wrong,use bash $0 --del user or bash $0 --del user1,user3..." exit else n=`echo $2| awk -F ‘,‘ ‘{print NF}‘` //用户个数 #用户个数大于1的,需要处理下创建,个数为1的直接删除 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 ] //参数为1直接报错退出 then echo "Wrong,use bash $0 --help" exit else echo "Use bash $0 --add username or bash $0 --add user1,user3... add user." echo " bash $0 --del username -r bash $0 --del user1,user3... delete user." echo " bash $0 --help print this info." fi ;; *) //其他情况,直接报错 告知情况 echo "Wrong,or bash $0 --del username or bash $0 --help" ;; esac 题目要求写一个脚本: 计算100以内所有能被3整除的正整数的和 参考答案#!/bin/bash sum=0 for i in `seq 1 100` do j=$[$i%3] if [ $j -eq 0 ] then sum=$[$sum+$i] fi done echo $sum 题目要求使用传参的方法写个脚本,实现加减乘除的功能。 要求:
参考答案#!/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 题目要求写一个脚本,执行后,打印一行提示“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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 如何在scala中没有找到密钥,如何使用密钥访问地图的值?
- angularjs – $mdDialog堆叠/嵌套对话框,是可能吗?
- linux文本处理工具及正则表达式集锦
- scala – 实现模式匹配的类
- 具有clipboardData属性的Angular2组件
- [Angular] Angular Custom Change Detection with ChangeDe
- 如何托管在同一服务器上公开端口80的两个Docker容器
- WebService处理大数据量数据出错:运行配置文件中指定的扩展
- Angular 2错误:未处理的Promise拒绝:模板解析错误:多个组
- Bootstrap+jQuery+Thinkphp+Mongodb实战开发社区交流网站平