shell脚本面试

1.kill掉mysql的脚本 

#!/bin/bash
while true
do
killall mysqld > /dev/null 2>&1
[ $? -ne 0 ] && echo "mysql_server is killall !" && break
sleep 1
done

2.监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。

#!/bin/bash
MYUSER=root
MYPASS="123456"
MAIL="1339329314@qq.com"
MYSOCK=/data/mysqld/mysql.sock
MYSQL_STARTUP="/etc/init.d/mysqld"
LOG_PATH=/var/log
LOG_FILE=${LOG_PATH}/mysqllogs_`date +%F`.log
MYSQL_PATH=/usr/local/mysql/bin
MYSQL_CMD="$MYSQL_PATH/mysql -u$MYUSER -p$MYPASS -S$MYSOCK"
###############################################################################
$MYSQL_CMD -e "select version ();" > /dev/null 2>&1
if [ $? -eq 0 ];then
echo "MYSQL is runing!"
exit 0
else
[ -x $MYSQL_STARTUP ] && $MYSQL_STARTUP start > $LOG_FILE
sleep 10

$MYSQL_CMD -e "select version ();" > /dev/null 2>&1

if [ $? -ne 0 ];then
for ((i=1;i<1001;i++))
do
killall -9 mysqld > /dev/null 2>&1
[ $? -ne 0 ] && break
sleep 1
done
[ -x $MYSQL_STARTUP ] && $MYSQL_STARTUP start >> $LOG_FLIE
fi

$MYSQL_CMD -e "select version ();" > /dev/null 2>&1 && status="restarted" || status="unknown"
echo "`date`-MYSQL status is $status" > $LOG_FILE
#mail -s "MYSQL status is $status" 1339329314@qq.com < $LOG_FILE
echo "`date`-MYSQL status is $status"|mail -s "mysqld" 1339329314@qq.com
fi

3.使用函数编写nginx启动脚本

#!/bin/bash
# chkconfig: - 99 20
# description:nginx
. /etc/init.d/functions

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

start(){
$PROG >& /dev/null
[ $? -eq 0 ] && action "nginx is started" /bin/true||
action "nginx is started" /bin/false
}
stop(){
kill -s QUIT $(cat $PIDF) >& /dev/null
[ $? -eq 0 ] && action "nginx is stop" /bin/true||
action "nginx is stop" /bin/false
}
restart(){
$0 stop >& /dev/null
$0 start >& /dev/null
[ $? -eq 0 ] && action "nginx is restart" /bin/true||
action "nginx is restart" /bin/false
}
reload(){
kill -s HUP $(cat $PIDF)
[ $? -eq 0 ] && action "nginx is reload" /bin/true||
action "nginx is reload" /bin/false
}
other(){
echo "useage:$0 {start|stop|restart|reload}"
exit 1
}

case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
*)
other
;;
esac
exit 0

4.普通的nginx启动脚本

#!/bin/bash
# chkconfig: - 99 20
# description:nginx
. /etc/init.d/functions

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case $1 in
start)
$PROG >& /dev/null
[ $? -eq 0 ] && action "nginx is started" /bin/true||
action "nginx is started" /bin/false
;;
stop)
kill -s QUIT $(cat $PIDF) >& /dev/null
[ $? -eq 0 ] && action "nginx is stop" /bin/true||
action "nginx is stop" /bin/false
;;
restart)
$0 stop >& /dev/null
$0 start >& /dev/null
[ $? -eq 0 ] && action "nginx is restart" /bin/true||
action "nginx is restart" /bin/false
;;
reload)
kill -s HUP $(cat $PIDF)
[ $? -eq 0 ] && action "nginx is reload" /bin/true||
action "nginx is reload" /bin/false
;;
*)
echo "useage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0

或者

#!/bin/bash
# chkconfig: - 99 20
# description:nginx

PROG="/usr/local/nginx/sbin/nginx"
PIDF="/usr/local/nginx/logs/nginx.pid"

case $1 in
start)
$PROG
;;
stop)
kill -s QUIT $(cat $PIDF)
;;
restart)
$0 stop &>> /dev/null
$0 start &>> /dev/null
;;
reload)
kill -s HUP $(cat $PIDF)
;;
*)
echo "useage:$0 {start|stop|restart|reload}"
exit 1
esac
exit 0

5.tomcat启动脚本

稍后补充

6.ping一个网段有哪些存活的IP脚本

[root@www 2017_08_31.sh]# cat panduan_wangduan_for_ip.sh
#!/bin/bash
IP=192.168.0.
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
#for i in $(seq 1 254)
for ((i=1;i<=254;i++))
do
ping $IP$i -c 2 | grep -q "ttl="
if [ $? -eq 0 ];then
echo "$IP$i yes !"
else
echo "$IP$i no !"
fi
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行结束......"
[root@www 2017_08_31.sh]# cat panduan_wangduan_while_ip.sh
#!/bin/bash
IP=192.168.0.
ip=1
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本开始执行......"
while [ $ip -lt 255 ]
do
ping $IP$ip -c 2|grep -q "ttl=" #&& echo "192.168.0.$ip yes" || echo "192.168.0.$ip no"
if [ $? -eq 0 ];then
echo "$IP$ip yes !"
else
echo "$IP$ip no !"
fi

#ip=$(($ip + 1))
ip=$(expr "$ip" "+" "1")
done
echo "`date "+%Y%m%d %H:%M:%S"`----->脚本执行结束......"

 7.判断成绩脚本

#!bin/bash
while true
do
read -t10 -p "请输入学号:" number
if [ $number == exit ];then
exit
fi
echo "$number"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
echo "this is number !"
else
exit
fi
read -t10 -p "请输入姓名:" name
echo "$name"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
exit
else
echo "this is character !"
fi
read -t10 -p "请输入成绩:" achievement
echo "$achievement"|[ -n "`sed -n '/^[0-9][0-9]*$/p'`" ]
if [ $? -eq 0 ];then
echo "this is number !"
else
exit
fi
if [ "$achievement" -ge "60" ];then
echo "学号=$number 姓名=$name 成绩=$achievement" >> pass.txt
else
echo "学号=$number 姓名=$name 成绩=$achievement" >> notpass.txt
fi
done

7.使用for循环在/html目录下通过随机小写10个字母加固定字符串html批量创建10个html文件

#!/bin/bash
file=/root/html
shell=/root/add_html_pl.sh
[ ! -d $file ] && mkdir $file || echo "$file 已经存在!"
[ ! -x $shell ] && exit 1||echo "$shell可以执行!"
for i in $(seq 1 10)
do
touch $file/$(mkpasswd -l 50 | tr -dc 'a-z'|cut -c1-10)_xyz.html
done

8.将以上文件名中的xyz全部改成xxx(用for循环实现),并且html改成大写。

#!/bin/bash
file=/root/html
shell=/root/html_file_xg.sh
[ ! -d $file ] && mkdir $file || echo "$file 已经存在!"
[ ! -x $shell ] && exit 1||echo "$shell可以执行!"
for i in $(ls -l /root/html/|awk '{print $9}'|grep -v "^$")
do
a=$(echo $i|cut -c 1-10)
mv $file/${a}_xyz.html $file/${a}_xxx.HTML
done

原文地址:https://www.cnblogs.com/xiaoyongzhuo/p/7484015.html