Shell exercise -17

Question requirements

Assuming that the root password of the current MySQL service is 123456, write a script to check whether the MySQL service is normal (for example, you can enter mysql normally to execute show processlist),

and check the current Whether the MySQL service is a master or a slave, if it is a slave, please judge whether its master-slave service is abnormal. If it is the Lord, nothing needs to be done.

Reference answer

#!/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}'`< br /> if [$n -eq 0 ]
then
echo "This is master."
else
echo "This is slave."
#Find out Slave_IO_Running and Slave_SQL_Running are yes or no
egrep'Slave_IO_Running:|Slave_SQL_Running:'/tmp/slave.stat|awk -F': ''{print $2}'> /tmp/SQL.tmp
# If it is no, it is down.
if grep -qw "No" /tmp/SQL.tmp
then
echo "The slave is down."
fi
fi
fi

Question requirements

Write a shell script that supports adding or deleting users. The specific requirements are as follows:

  1. Only Three options are supported:’–del’,’–add’,’–help’, and an error will be reported when other options are entered.
  2. When using ‘–add’, you need to verify whether the user name exists. If it exists, the feedback exists, and it will not be added. If it does not exist, create the user, and you need to set the same password as the user name.
  3. When using ‘–del’, you need to verify whether the user name exists, and if it exists, delete the user and its home directory. If it doesn’t exist, it will report that the user does not exist. ?
  4. The –help option gives feedback on how to use it.
  5. Echo $? can be used to detect the execution of the script. If the user is successfully deleted or added, it is 0, and if it is unsuccessful, it is a non-zero positive integer.
  6. It can be separated by commas to add or delete multiple users at once. For example adddel.sh –add user1,user2,user3

Refer to the answer

#!/bin/baash

#Judging parameters, Zero parameters or more than two parameters, will report an error and exit
if [$# -eq 0] || [$# -gt 2 ]
then
echo "Wrong, use bash $0- -add username, or bash $0 --del username or bash $0 --help"
exit
fi

#useradduser function
ex_user()
{
if! id $1 2>/dev/null >/dev/null
then
useradd $1 && echo "$1 add successful."
else
echo $1 exist.
fi
}

#Delete user
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] //If the parameter is 1, report an error and exit directly
then
echo "Wrong, use bash $0 --add user or bash $0 --add user1,user2,user3..."
exit
else
n=`echo $2| awk -F',''{print NF}'` //Number of users
#Users whose number is greater than 1, need to be polled and created, and those equal to 1 are created directly
if [$n -gt 1 ]
then
for i in `seq 1 $n`
do
username=`echo $2 |awk -vj=$i -F',''{print $j}'`
ex_user $username
done
else
ex_user $2
fi
fi
;;
--del)
if [$# -eq 1] //The parameter is 1 directly report an error Exit
then
echo "Wrong, use bash $0 --del user or bash $0 --del user1,user2,user3..."
exit
else
n=`echo $2| awk -F',''{print NF}'` //Number of users
#If the number of users is greater than 1, it needs to be created under processing, and the number of users is directly deleted.
if [$n -gt 1 ]
then
for i in `seq 1 $n`
do
username=`echo $2 |awk -vj=$i- F',''{print $j}'`
notex_user $username
done
else
notex_user $2
fi< br /> fi
;;
--help)
if [$# -ne 1] //If the parameter is 1, report an error and exit directly
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
;;
*) //In other cases, directly report the error to inform the situation
echo "Wrong, use bash $0 --add username, or bash $0 --del username or bash $0 --help"
;;
esac

Question requirements

Write a script: Calculate the sum of all positive integers within 100 that are divisible by 3

Reference answer

#!/bin/bash
sum=0
for i in `seq 1 100`
do
j=$[$i%3]
if [$j -eq 0 ]
then
sum=$[$sum+$i]
fi
done< br />echo $sum

Question requirements

Use the method of passing parameters to write a script to realize the function of addition, subtraction, multiplication and division.
For example:??sh??a.sh??1???2, this will calculate the results of addition, subtraction, multiplication, and division respectively.

Requirements:

  1. The script needs to determine that the two numbers provided must be integers
  2. When doing subtraction or division, you need to determine which number is larger, and subtract When you need to use a large number to reduce the number, you need to divide the large number by the small number when dividing, and the result needs to retain two decimal points.

Reference answer

#!/bin/bash
is_nu()
{
n=`echo $1 |sed ' s/[0-9]//g'`
if [-n "$n" ]
then
echo "The parameter given must be a positive integer"
exit< br /> fi
}
if [$# -ne 2 ]
then
echo "Two parameters must be entered"
exit
else< br /> 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

Question requirements

Write a script. After execution, print a line prompting “Please input a number:” to ask the user to enter a number, then print out the number, and then ask the user to enter the number again. Stop until the user enters “end”.

Reference answer

#!/bin/bash
while :
do
read -p "Please input a number: "n
if [-z "$n" ]
then
echo "Please enter a pure number."
continue
fi
if echo $n |grep- qi'end'
then
exit
fi
n1=`echo $n|sed's/[0-9]//g'`
if [ -n "$n1" ]
then
echo "Please enter a pure number."
continue
else
echo "The number you entered is: $n"
continue
fi
done

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 5512 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.