企业真实shell面试题,一起来挑战把!

声明:如有雷同,纯属抄袭

 

1、开发一个守护进程脚本,每30秒执行一次,检查mysql主从同步

 

[root@xiaoya scripts]# cat check_mysql_slave_status.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail 402753795@qq.com
# function check_mysql_slave_status
# version 4.1.2

####do it behind start mysql####
#grep "slave-skip-errors = " /data/3307/my.cnf |awk -F "[ ]" '{print $3}'|sed -i "s#.*#1158\,1159\,1007\,1008\,1062#g"
####define $IP $DATE####
IP=$(ifconfig eth0|awk '{if(NR==2)print $0}'|awk -F "[ :]+" '{print $4}')
DATE=$(date "+%F %H:%M:%S")
####check mysql_slave_status####
mysql_slave_status=$(mysql -uroot -p123456 -S /data/3307/mysql.sock -e "show slave status\G"|sed -n '12,13p'|awk -F ":" '{print $2}'|uniq -c)
if [ "$mysql_slave_status" == "      2  Yes" ]
then
  echo "mysql slave is runing ok"
else
  echo "#####################$DATE $IP##########################" >>~/oldboy.txt
  echo "warn:mysql slave is not runing " >>~/oldboy.txt && mail -s "check_mysql_slave_status" 402753795@qq.com <~/oldboy.txt
fi

[root@xiaoya scripts]# cat do.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail 402753795@qq.com
# function do check_mysql_slave_status
# version 4.1.2

while true
do
/bin/sh /server/scripts/check_mysql_slave_status.sh
sleep 30

done

 

2、用for循环随机生成10个字母加固定字段的10个html文件

 

[root@xiaoya scripts]# cat touch_html.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail 402753795@qq.com
# function touch html
# version 4.1.2

A=$(head -c 500 /dev/urandom |tr -dc a-z|head -c 10)
B=_oldboy.html
cd ~/oldboy
for i in {1..10}
do
A=$(head -c 500 /dev/urandom |tr -dc a-z|head -c 10)
touch $A$B-$i
mv $A$B-$i $A$B
done

 

 3、将以上文件中的oldboy全部替换为oldgirl,并将html换成HTML(用for循环)

法1):

[root@xiaoya scripts]# cat change_touch_html.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail 402753795@qq.com
# function change touch html
# version 4.1.2

for n in `ls ~/oldboy`
do
A=$(echo $n |sed "s#oldboy#oldgirl#g"|sed "s#html#HTML#g")
cd ~/oldboy
mv $n $A
done

法2):

[root@xiaoya scripts]# cat change_touch_html1.sh
#!/bin/bash
# date 2016-1-5
# author tom
# mail 402753795@qq.com
# function change touch html
# version 4.1.2

for n in `ls ~/oldboy`
do
A=$(echo $n|cut -c 12-23|sed 's#oldgirl.HTML#oldboy.html#g')
B=$(echo $n|cut -c 1-11)
#A=$(echo $n |sed "s#oldboy#oldgirl#g"|sed "s#html#HTML#g")
cd ~/oldboy
mv $n $B$A
done

 

补充:

查看定时任务的执行情况[root@xiaoya scripts]# tail -f /var/log/cron