SHELL训练营--day15_shell练习26-30

#监控mysql服务
#!/bin/bash
mysql="/usr/local/mysql/bin/mysql -uroot -p123456"

if $mysql -e "show processlist " &>/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`
    if [ $n -eq 0 ]
    then
        echo "This is master"
    else
        echo "This is slave."
        egrep 'Slave_IO_Running:|Slave_SQL_Running:' /tmp/slave.stat |awk -F ':' '{print $2}' >/tmp/SQL.tmp
        if grep -qw "NO" /tmp/SQL.tmp
        then
            echo "The slave is down."
        fi
    fi
fi 

#删除用户
#!/bin/bash
if [ $# -eq 0 ]
then
    echo "Wrong.use \"bash $0 --add username\",or\"bash $0 --del username\",or\"bash $0 --help\"."
    exit
fi

user_exist=0 #0用户不存在,1用户存在。
ex_user()
{
    if ! id $1  &>/dev/null
    then
        echo "$1 not exist."
        user_exist=0
    else
        user_exist=1
    fi
}

case $1 in 
    --add)
        if [ $# -eq 1 ]
        then
            echo "Wrong.use \"bash $0 --add username\",or\"bash $0 --del username\",or\"bash $0 --help\"."
            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
                    if [ user_exist -eq 0 ]
                    then
                        useradd $username
                    fi
                done
            else
                useradd $2
            fi
        fi
        ;;

    --del)
        if [ $# -gt 2 ]
        then
            echo "Wrong.use \"bash $0 --add username\",or\"bash $0 --del username\",or\"bash $0 --help\"."
            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
                    if [ user_exist -eq 1 ]
                    then
                        userdel $username
                    fi
                done
            else
                userdel $2
            fi
        fi
        ;;
    --help|*)
        echo "use \"bash $0 --add username\",or\"bash $0 --del username\",or\"bash $0 --help\"."
        ;;

#计算和
#!/bin/bash
sum=0
for i in `seq 1 100`
do
    m=$[$i%3]
    if [ $m -eq 0 ]
    then
        sum=$[$sum+$i]
    fi
done

echo "和是: $sum "

#加减乘除
#!/bin/bash

is_num()
{
    n=`echo $1|sed 's/[0-9]//g'`
    if [ -n "$n" ]
    then 
        echo "请输入正确数字。"
    fi
}

is_big=0    #0表示大于
big()
{
    if [ $# -eq 2 ]
    then
        echo "请输入两个数字。"
    elif [ $1 -gt $2 ]
    then
        is_big=0
    else
        is_big=1
    fi
}

add()
{
    sum=$[$1+$2]
    echo "$1+$2=$sum"
}

subtract()
{
    if [ $is_big -eq 0 ]
    then
        cha=$[$1-$2]
    else
        cha=$[$2-$1]
    fi
}

multiply()
{
    ji=$[$1*$2]
}

divide()
{
    if [ $is_big -eq 0 ]
    then
        echo "scale=2;$1/$2|bc"
    else
        echo "scale=2;$2/$1|bc"
    fi
}

if [ $# -ne 2 ]
then
    echo "需要输入两个参数。"
    exit
else
    is_num $1
    is_num $2
fi

add $1 $2
subtract $1 $2
multiply $1 $2
divide $1 $2

#输入数字
#!/bin/bash
while:
do
    read -p "Please input a number:" n
    if [ -n "$n" ]
    then 
        echo "请输入一个数字。"
        continue
    fi

    if echo $n |grep -ql 'end'
    then
        exit
    fi

    n1=`echo $n| sed 's/[0-9]//g'|wc -l`
    if [ $n1 -ne 0 ]
    then
        echo "清输入纯数字。"
        continue
    else
        echo "你输入的数字是:$n"
    fi
done

猜你喜欢

转载自blog.51cto.com/sincethen/2340888