shell习题100(六)

版权声明:版权所有,转载请注明出处。email:[email protected] https://blog.csdn.net/qq_42334372/article/details/86183109

题目要求

假设,当前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 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

题目要求

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

  1. 只支持三个选项:’–del’,’–add’,’–help’,输入其他选项报错。
  2. 使用’–add’时,需要验证用户名是否存在,存在则反馈存在,且不添加。 不存在则创建该用户,需要设置与该用户名相同的密码。
  3. 使用’–del’时,需要验证用户名是否存在,存在则删除用户及其家目录。不存在则反馈该用户不存在。
  4. –help选项反馈出使用方法。
  5. 能用echo $?检测脚本执行情况,成功删除或添加用户为0,不成功为非0正整数。
  6. 能以英文逗号分割,一次性添加或者删除多个用户。例如 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

题目要求

写一个脚本: 计算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

题目要求

使用传参的方法写个脚本,实现加减乘除的功能。
例如:  sh  a.sh  1   2,这样会分别计算加、减、乘、除的结果。

要求:

  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

题目要求

写一个脚本,执行后,打印一行提示“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

猜你喜欢

转载自blog.csdn.net/qq_42334372/article/details/86183109