@老男孩Shell企业面试题

shell 
2016年9月7日 
本文来自于老男孩教育,未经本人同意,禁止转载!否则追究法律责任。 
原文:http://oldboy.blog.51cto.com/2561410/1632876


企业面试题1:

  (生产实战案例):监控MySQL主从同步是否异常,如果异常,则发送短信或者邮件给管理员。提示:如果没主从同步环境,可以用下面文本放到文件里读取来模拟: 
阶段1:开发一个守护进程脚本每30秒实现检测一次。 
阶段2:如果同步出现如下错误号(1158,1159,1008,1007,1062),则跳过错误。 
阶段3:请使用数组技术实现上述脚本(获取主从判断及错误号部分)

  1. [root@oldboy~]# mysql -uroot -p'oldboy' -S /data/3307/mysql.sock -e "show slavestatus\G;"
  2. *************************** 1. row ***************************
  3. Slave_IO_State:Waiting for master to send event
  4. Master_Host:10.0.0.179 #当前的mysql master服务器主机
  5. Master_User: rep
  6. Master_Port: 3306
  7. Connect_Retry: 60
  8. Master_Log_File:mysql-bin.000013
  9. Read_Master_Log_Pos: 502547
  10. Relay_Log_File:relay-bin.000013
  11. Relay_Log_Pos:251
  12. Relay_Master_Log_File:mysql-bin.000013
  13. Slave_IO_Running:Yes
  14. Slave_SQL_Running: Yes
  15. Replicate_Do_DB:
  16. Replicate_Ignore_DB: mysql
  17. Replicate_Do_Table:
  18. Replicate_Ignore_Table:
  19. Replicate_Wild_Do_Table:
  20. Replicate_Wild_Ignore_Table:
  21. Last_Errno: 0
  22. Last_Error:
  23. Skip_Counter: 0
  24. Exec_Master_Log_Pos: 502547
  25. Relay_Log_Space:502986
  26. Until_Condition:None
  27. Until_Log_File:
  28. Until_Log_Pos: 0
  29. Master_SSL_Allowed: No
  30. Master_SSL_CA_File:
  31. Master_SSL_CA_Path:
  32. Master_SSL_Cert:
  33. Master_SSL_Cipher:
  34. Master_SSL_Key:
  35. Seconds_Behind_Master: 0 #和主库比同步延迟的秒数,这个参数很重要
  36. Master_SSL_Verify_Server_Cert: No
  37. Last_IO_Errno: 0
  38. Last_IO_Error:
  39. Last_SQL_Errno: 0
  40. Last_SQL_Error:

本脚本由李佳豪同学分享

  1. [root@db02 tmp]# cat MySQL主从监控.sh
  2. #!/bin/bash
  3. Mysql="mysql -uroot -poldboy123 -S /data/3307/mysql.sock"
  4. L_aaa(){
  5. Status=`$Mysql -e "show slave status\G" | grep -E "_Running: Yes|Seconds_Behind_Master: [0-2]" | wc -l`
  6. Code=`$Mysql -e "show slave status\G" | awk '/Last_SQL_Errno:/{print $2}'`
  7. }
  8. L_Status(){
  9. [ $Status -ne 3 ] && {
  10. return 1
  11. } || {
  12. return 0
  13. }
  14. }
  15. S_Code=(
  16. 1158
  17. 1159
  18. 1008
  19. 1007
  20. 1062
  21. )
  22. L_Skip(){
  23. [ $Code -eq 0 ] && return 0
  24. for i in ${S_Code[*]}
  25. do
  26. [ $Code -eq $i ] && {
  27. $Mysql -e "stop slave;set global sql_slave_skip_counter = 1;start slave;" && \
  28. return 0
  29. }
  30. done
  31. return 1
  32. }
  33. main(){
  34. while true
  35. do
  36. L_aaa
  37. L_Skip
  38. Error1=$?
  39. L_Status
  40. Error2=$?
  41. [ $Error1 -eq 1 -o $Error2 -eq 1 ] && echo "Error"
  42. done
  43. }
  44. main

企业面试题2:

  使用for循环在/oldboy目录下通过随机小写10个字母加固定字符串oldboy批量创建10个html文件,名称例如为:

  1. [root@oldboy oldboy]# sh /server/scripts/oldboy.sh
  2. [root@oldboy oldboy]# ls
  3. coaolvajcq_oldboy.html qnvuxvicni_oldboy.html vioesjmcbu_oldboy.html
  4. gmkhrancxh_oldboy.html tmdjormaxr_oldboy.html wzewnojiwe_oldboy.html
  5. jdxexendbe_oldboy.html ugaywanjlm_oldboy.html xzzruhdzda_oldboy.html
  6. qcawgsrtkp_oldboy.html vfrphtqjpc_oldboy.html

脚本如下:

  1. [root@db02 ~]# sh html.sh
  2. [root@db02 ~]# ls /oldboy/
  3. aebccciiaj_oldboy.html fffabecgbc_oldboy.html
  4. afffebcchb_oldboy.html ffghcffegb_oldboy.html
  5. dbccddabbj_oldboy.html hffbhfgdff_oldboy.html
  6. ehbdaedach_oldboy.html jadafhbaaf_oldboy.html
  7. fbaacihehi_oldboy.html jgfebjbebd_oldboy.html
  8. [root@db02 ~]# cat html.sh
  9. #!/bin/bash
  10. . /etc/init.d/functions
  11. [ -d /oldboy ]|| mkdir -p /oldboy
  12. cd /oldboy
  13. for i in `seq 10`
  14. do
  15. touch `echo $RANDOM|md5sum|cut -c 1-10|tr "[0-9]" "[a-z]"`_oldboy.html
  16. done

企业面试题3:

请用至少两种方法实现! 
  将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。 
第一种方法:

  1. [root@db02 ~]# sh html1.sh
  2. [root@db02 ~]# ll /oldboy/
  3. total 0
  4. -rw-r--r-- 1 root root 0 Jul 20 20:45 aebccciiaj_oldboy.HTML
  5. -rw-r--r-- 1 root root 0 Jul 20 20:45 afffebcchb_oldboy.HTML
  6. -rw-r--r-- 1 root root 0 Jul 20 20:45 dbccddabbj_oldboy.HTML
  7. -rw-r--r-- 1 root root 0 Jul 20 20:45 ehbdaedach_oldboy.HTML
  8. -rw-r--r-- 1 root root 0 Jul 20 20:45 fbaacihehi_oldboy.HTML
  9. -rw-r--r-- 1 root root 0 Jul 20 20:45 fffabecgbc_oldboy.HTML
  10. -rw-r--r-- 1 root root 0 Jul 20 20:45 ffghcffegb_oldboy.HTML
  11. -rw-r--r-- 1 root root 0 Jul 20 20:45 hffbhfgdff_oldboy.HTML
  12. -rw-r--r-- 1 root root 0 Jul 20 20:45 jadafhbaaf_oldboy.HTML
  13. -rw-r--r-- 1 root root 0 Jul 20 20:45 jgfebjbebd_oldboy.HTML
  14. [root@db02 ~]# cat html1.sh
  15. #!/bin/bash
  16. cd /oldboy
  17. for i in `cd /oldboy`
  18. do
  19. rename "oldboy" "oldgirl" *.html|rename "html" "HTML" *.html
  20. done

第二种方法:

  1. [root@db02 ~]# sh html2.sh
  2. [root@db02 ~]# ll /oldboy/
  3. total 0
  4. -rw-r--r-- 1 root root 0 Jul 20 20:45 aebccciiaj_oldgirl.HTML
  5. -rw-r--r-- 1 root root 0 Jul 20 20:45 afffebcchb_oldgirl.HTML
  6. -rw-r--r-- 1 root root 0 Jul 20 20:45 dbccddabbj_oldgirl.HTML
  7. -rw-r--r-- 1 root root 0 Jul 20 20:45 ehbdaedach_oldgirl.HTML
  8. -rw-r--r-- 1 root root 0 Jul 20 20:45 fbaacihehi_oldgirl.HTML
  9. -rw-r--r-- 1 root root 0 Jul 20 20:45 fffabecgbc_oldgirl.HTML
  10. -rw-r--r-- 1 root root 0 Jul 20 20:45 ffghcffegb_oldgirl.HTML
  11. -rw-r--r-- 1 root root 0 Jul 20 20:45 hffbhfgdff_oldgirl.HTML
  12. -rw-r--r-- 1 root root 0 Jul 20 20:45 jadafhbaaf_oldgirl.HTML
  13. -rw-r--r-- 1 root root 0 Jul 20 20:45 jgfebjbebd_oldgirl.HTML
  14. [root@db02 ~]# cat html2.sh
  15. #!/bin/bash
  16. cd /oldboy
  17. for i in `ls *.HTML`
  18. do
  19. mv $i `echo $i|sed -e 's#oldboy#oldgirl#g;s#html#HTML#g'`
  20. done

补充:QQ 1163718639

  1. [root@db01 shell30]# cat 3.sh
  2. #!/bin/bash
  3. for i in /oldboy/* do rename oldboy. oldgir. $i|echo oldgirlok done for x in /oldboy/* do rename html HTML $x|echo htmlok done
  4. [root@db01 shell30]#

企业面试题4:

  批量创建10个系统帐号oldboy01-oldboy10并设置密码(密码为随机8位字符串)。

  1. [root@db02 tmp]# cat useradd.sh
  2. #!/bin/bash
  3. [ $UID -ne 0 ]&& echo "only root run"&&exit 1
  4. [ -f /etc/init.d/functions ]&& . /etc/init.d/functions
  5. isexist() {
  6. result=$(grep -w "^$1" /etc/passwd|wc -l)
  7. if [ $result -ne 0 ];then
  8. echo "user $1 is exist!!"
  9. ret 1 "create user is "
  10. continue
  11. fi
  12. }
  13. ret() {
  14. if [ $1 -eq 0 ];then
  15. action "$2" /bin/true
  16. else
  17. action "$2" /bin/false
  18. fi
  19. }
  20. create() {
  21. for i in $(seq -w 10)
  22. do
  23. user="oldboy$i"
  24. isexist $user
  25. pass=$(cat /proc/sys/kernel/random/uuid|md5sum|cut -c 1-10)
  26. useradd $user&&echo $pass|passwd --stdin $user &>/dev/null
  27. ret $? "crate user $user"
  28. echo "$user $pass" >> /tmp/user.list
  29. done
  30. }
  31. main() {
  32. create
  33. }
  34. main

企业面试题5:

  写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些(方法有很多) 
  本脚本由刘沈晨分享 
注意:此脚本使用nmap,如果没有需要使用yum -y install nmap

  1. [root@db02 scripts]# cat test_5.sh
  2. #!/bin/sh
  3. #
  4. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  5. function IP_count(){
  6. for n in 10.0.0.{0..255}
  7. do
  8. IP_check=`nmap -sP $n|grep "Host is up"|wc -l`
  9. if [ ${IP_check} -eq 1 ];then
  10. action "$n" /bin/true
  11. let i+=1
  12. fi
  13. done
  14. }
  15. function main(){
  16. IP_count
  17. echo "The total number of online IP Addresses is " $i
  18. }
  19. main

企业实战题6:

  写一个脚本解决DOS攻击生产案例 
提示:根据web日志或者或者网络连接数,监控当某个IP并发连接数或者短时内PV达到100,即调用防火墙命令封掉对应的IP,监控频率每隔3分钟。防火墙命令为:iptables -I INPUT -s 10.0.1.10 -j DROP。 
  本脚本使用测试文件进行编写:模拟文件下载 链接:http://pan.baidu.com/s/1gfqeHaz 密码:3usn

  1. [root@db02 scripts]# cat test_6.sh
  2. #!/bin/sh
  3. #
  4. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  5. IP_file="/server/scripts/ddos.txt"
  6. IP_filter_command="iptables -I INPUT -j DROP -s"
  7. IP_recover_command="iptables -D INPUT -j DROP -s"
  8. function IP_check(){
  9. grep "EST" ${IP_file}|awk -F "[ |:]+" '{print $6}'|sort |uniq -c|sort -rn -k1 > /server/scripts/ip.txt
  10. }
  11. function IP_filter(){
  12. exec < /server/scripts/ip.txt
  13. while read line
  14. do
  15. IP_count=`echo $line|awk '{print $1}'`
  16. IP=`echo $line|awk '{print $2}'`
  17. IP_fil=`iptables -L -n|grep "\b${IP}\b"|wc -l`
  18. if [ ${IP_count} -gt 25 -a ${IP_fil} -eq 0 ];then
  19. ${IP_filter_command} ${IP}
  20. echo "${IP}" >> /server/scripts/ip_filtered.txt
  21. action "Filter ${IP}" /bin/true
  22. fi
  23. done
  24. }
  25. function IP_recover(){
  26. exec < /server/scripts/ip.txt
  27. while read line
  28. do
  29. IP_count=`echo $line|awk '{print $1}'`
  30. IP=`echo $line|awk '{print $2}'`
  31. IP_fil=`iptables -L -n|grep "\b${IP}\b"|wc -l`
  32. if [ ${IP_count} -le 25 -a ${IP_fil} -eq 1 ];then
  33. ${IP_recover_command} ${IP}
  34. echo "${IP}" >> /server/scripts/ip_filtered.txt
  35. action "Recover ${IP}" /bin/true
  36. fi
  37. done
  38. }
  39. function main(){
  40. case "$1" in
  41. filter)
  42. IP_check
  43. echo "$(date +%F-%H:%M:%S) filtered by $(whoami)" >> /server/scripts/ip_filtered.txt
  44. IP_filter
  45. ;;
  46. recover)
  47. IP_check
  48. echo "$(date +%F-%H:%M:%S) recovered by $(whoami)" >> /server/scripts/ip_filtered.txt
  49. IP_recover
  50. ;;
  51. *)
  52. echo "USAGE:$0 {filter|recover}"
  53. exit 1
  54. esac
  55. }
  56. main $*

企业实战题7:

  开发mysql多实例启动脚本: 
已知mysql多实例启动命令为:mysqld_safe--defaults-file=/data/3306/my.cnf & 
停止命令为:mysqladmin -u root -poldboy123 -S /data/3306/mysql.sockshutdown 
请完成mysql多实例启动启动脚本的编写 
  要求:用函数,case语句、if语句等实现

  1. 1.第一种方法:比较简单,适用于一台MySQL服务器上
  2. [root@db02 mysql]# cat mysql.sh
  3. #!/bin/bash
  4. ###########################
  5. #I am YuhongCong
  6. #qq:604419314
  7. ##########################
  8. [ -f /etc/init.d/functions ]&& . /etc/init.d/functions
  9. #config
  10. prot=3306
  11. mysql_user=root
  12. mysql_paswd=123456
  13. Path=/data/3306/
  14. mysql_start(){
  15. if [ -f ${Path}mysqld.pid ];then
  16. echo "MySQL is running"
  17. exit 1
  18. else
  19. mysqld_safe --defaults-file=${Path}my.cnf &>/dev/null &
  20. if [ $? -eq 0 ];then
  21. action "MySQL is starting" /bin/true
  22. else
  23. action "MySQL no start" /bin/false
  24. fi
  25. fi
  26. }
  27. mysql_stop(){
  28. if [ ! -f ${Path}mysqld.pid ];then
  29. echo "Mysql not running"
  30. else
  31. mysqladmin -u ${mysql_user} -p${mysql_paswd} -S ${Path}mysql.sock shutdown
  32. if [ $? -eq 0 ];then
  33. action "mysql is stop" /bin/true
  34. else
  35. action "mysql no stop" /bin/false
  36. fi
  37. fi
  38. }
  39. case "$1" in
  40. start)
  41. mysql_start
  42. ;;
  43. stop)
  44. mysql_stop
  45. ;;
  46. restart)
  47. mysql_stop
  48. sleep 3
  49. mysql_start
  50. ;;
  51. *)
  52. echo "Please input {start|stop|restart}"
  53. ;;
  54. esac

第二种方法:应强哥及李啸宇、大欣等要求进行改版。 
  适用于管理多实例启动的脚本

  1. [root@db02 tmp]# cat mysqlgood.sh
  2. #!/bin/bash
  3. ##################
  4. #I am yuhongcong
  5. #qq:604419314
  6. #################
  7. [ -f /etc/init.d/functions ]&& . /etc/init.d/functions
  8. prot=3306
  9. mysql_user=root
  10. mysql_paswd=123456
  11. Path=/data/3306/
  12. #############################################
  13. cat <<END
  14. ############################
  15. # 3306
  16. # 3307
  17. # exit
  18. ###########################
  19. END
  20. read -p "Please input {3306|3307}" a
  21. mysql1_start(){
  22. if [ -f ${Path}mysqld.pid ];then
  23. echo "MySQL is runningx"
  24. else
  25. mysqld_safe --defaults-file=${Path}my.cnf &>/dev/null &
  26. if [ $? -eq 0 ];then
  27. action "mysql 3306 is starting" /bin/true
  28. else
  29. action "mysql 3306 not start" /bin/false
  30. fi
  31. fi
  32. }
  33. mysql1_stop(){
  34. if [ ! -f ${Path}mysqld.pid ];then
  35. echo "mysql 3306 not running"
  36. else
  37. mysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3306/mysql.sock shutdown
  38. if [ $? -eq 0 ];then
  39. action "mysql 3306 is stopping" /bin/true
  40. else
  41. action "mysql 3306 no stop" /bin/false
  42. fi
  43. fi
  44. }
  45. mysql2_start(){
  46. if [ -f /data/3307/mysqld.pid ];then
  47. echo "MySQL 3307 is runningx"
  48. else
  49. mysqld_safe --defaults-file=/data/3307/my.cnf &>/dev/null &
  50. if [ $? -eq 0 ];then
  51. action "mysql 3307 is starting" /bin/true
  52. else
  53. action "mysql 3307 not start" /bin/false
  54. fi
  55. fi
  56. }
  57. mysql2_stop(){
  58. if [ ! -f /data/3307/mysqld.pid ];then
  59. echo "mysql 3307 not running"
  60. else
  61. mysqladmin -u ${mysql_user} -p${mysql_paswd} -S /data/3307/mysql.sock shutdown
  62. if [ $? -eq 0 ];then
  63. action "mysql 3307 is stopping" /bin/true
  64. else
  65. action "mysql 3307 no stop" /bin/false
  66. fi
  67. fi
  68. }
  69. aaa(){
  70. cat <<END
  71. ###########################
  72. # 1.start
  73. # 2.stop
  74. # 3.restart
  75. # 4.exit
  76. ###########################
  77. END
  78. read -p "please input {1|2|3|4}" b
  79. }
  80. bbb(){
  81. cat <<END
  82. ###########################
  83. # 1.start
  84. # 2.stop
  85. # 3.restart
  86. # 4.exit
  87. ###########################
  88. END
  89. read -p "please input {1|2|3|4}" c
  90. }
  91. case "$a" in
  92. 3306)
  93. aaa
  94. ;;
  95. 3307)
  96. bbb
  97. ;;
  98. exit)
  99. exit
  100. ;;
  101. esac
  102. case "$b" in
  103. 1)
  104. mysql1_start
  105. ;;
  106. 2)
  107. mysql1_stop
  108. ;;
  109. 3)
  110. mysql1_stop
  111. sleep 3
  112. mysql1_start
  113. ;;
  114. 4)
  115. exit 2
  116. ;;
  117. esac
  118. case "$c" in
  119. 1)
  120. mysql2_start
  121. ;;
  122. 2)
  123. mysql2_stop
  124. ;;
  125. 3)
  126. mysql2_stop
  127. sleep 3
  128. mysql2_start
  129. ;;
  130. 4)
  131. exit 2
  132. ;;
  133. esac

企业实战题8:

  如何实现对MySQL数据库进行分库备份,请用脚本实现 
  本文由刘康同学提供

  1. [root@db02 tmp]# cat MoreDatabasesBak.sh
  2. #!/bin/bash
  3. . /etc/init.d/functions
  4. PORT='3306'
  5. BAKUPDIR='/server/backup'
  6. MYSQLUSER='root'
  7. MYSQLPASS='oldboy'
  8. SOCK="/data/${PORT}/mysql.sock"
  9. CMDDIR="/application/mysql/bin"
  10. MYSQL="${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"
  11. DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`
  12. AYYAYDB=($DBNAME)
  13. MYSQLDUMP="${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"
  14. function BAKDB(){
  15. for((n=0;n<${#AYYAYDB[*]};n++))
  16. do
  17. ${MYSQLDUMP} --events -B ${AYYAYDB[$n]} |gzip >${BAKUPDIR}/${AYYAYDB[$n]}_$(date +%T-%F)_bak.sql.gz
  18. RETVAL=$?
  19. if [ $RETVAL -eq 0 ]
  20. then
  21. echo "${AYYAYDB[$n]} bak successfull `date +%F-%T` " >>/tmp/DBbakstatus.log
  22. else
  23. echo "${AYYAYDB[$n]} bak fail `date +%F-%T` " >>/tmp/DBbakstatus.log
  24. fi
  25. done
  26. mail -s "DB STATUS" www.abcdocker.com@qq.com < /tmp/DBbakstatus.log
  27. return
  28. }
  29. function DBstatus(){
  30. [ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p
  31. ${MYSQL} -e "show full processlist;" &> /dev/null
  32. RETVAL=$?
  33. if [ $RETVAL -eq 0 ]
  34. then
  35. >/tmp/DBbakstatus.log
  36. BAKDB
  37. else
  38. echo "DB BAD!!! `date +%F-%T`" | mail -s "DB BAD!!!" www.abcdocker.com@qq.com
  39. exit
  40. fi
  41. }
  42. DBstatus

企业实战题9:

  如何实现对MySQL数据库进行分库加分表备份,请用脚本实现 
本脚本还是由刘康同学提供

  1. [root@db02 tmp]# cat MoreTablesBak.sh
  2. #!/bin/bash
  3. . /etc/init.d/functions
  4. PORT='3306'
  5. BAKUPDIR='/server/backup'
  6. MYSQLUSER='root'
  7. MYSQLPASS='oldboy'
  8. SOCK="/data/${PORT}/mysql.sock"
  9. CMDDIR="/application/mysql/bin"
  10. MYSQL="${CMDDIR}/mysql -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"
  11. DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`
  12. AYYAYDB=($DBNAME)
  13. MYSQLDUMP="${CMDDIR}/mysqldump -u${MYSQLUSER} -p${MYSQLPASS} -S${SOCK}"
  14. function BAKDB(){
  15. DBNAME=`${MYSQL} -e "show databases;"|sed 1d|egrep -v "_schema|mysql"`
  16. AYYAYDB=($DBNAME)
  17. for((n=0;n<${#AYYAYDB[*]};n++))
  18. do
  19. TABLE_BAK_DIR="${BAKUPDIR}/${AYYAYDB[$n]}"
  20. TABLENAME=`${MYSQL} -e "show tables from ${AYYAYDB[$n]};"|sed 1d`
  21. ARRAYTABLE=(${TABLENAME})
  22. for((i=0;i<${#ARRAYTABLE[*]};i++))
  23. do
  24. [ -d ${TABLE_BAK_DIR} ] || mkdir ${TABLE_BAK_DIR} -p
  25. ${MYSQLDUMP} ${AYYAYDB[$n]} ${ARRAYTABLE[$i]} |gzip >${TABLE_BAK_DIR}/${ARRAYTABLE[$i]}_$(date +%T-%F)_bak.sql.gz
  26. RETVAL=$?
  27. if [ $RETVAL -eq 0 ]
  28. then
  29. echo "${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak successfull `date +%F-%T` " >>/tmp/DB_table_bakstatus.log
  30. else
  31. echo "${AYYAYDB[$n]}_${ARRAYTABLE[$i]} bak fail `date +%F-%T` " >>/tmp/DB_table_bakstatus.log
  32. fi
  33. done
  34. done
  35. mail -s "DB STATUS" www.abcdocker.com@qq.com < /tmp/DB_table_bakstatus.log
  36. return
  37. }
  38. function DBstatus(){
  39. [ -d ${BAKUPDIR} ] || mkdir ${BAKUPDIR} -p
  40. ${MYSQL} -e "show full processlist;" &> /dev/null
  41. RETVAL=$?
  42. if [ $RETVAL -eq 0 ]
  43. then
  44. >/tmp/DB_table_bakstatus.log
  45. BAKDB
  46. else
  47. echo "DB BAD!!! `date +%F-%T`" | mail -s "DB BAD!!!" www.abcdocker.com@qq.com
  48. exit
  49. fi
  50. }
  51. DBstatus

企业面试题10:

请用至少两种方法实现! 
bash for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。 
I am oldboy teacher welcome to oldboy training class.

  1. 没讲数组之前自己做的:
  2. [root@web02 ~]# cat /server/scripts/28/bash.sh
  3. #!/bin/bash
  4. for n in `echo I am oldboy teacher welcome to oldboy training class.`
  5. do
  6. AAA=$( echo $n|wc -c)
  7. if [ $AAA -gt 7 ];then
  8. continue;
  9. fi
  10. echo $n
  11. done

第一种方法:不使用数组

  1. [root@db02 scripts]# cat 3.sh
  2. #!/bin/bash
  3. for i in I am oldboy teacher welcome to oldboy training class.
  4. do
  5. if [ "${#i}" -le 6 ]
  6. then
  7. echo $i
  8. fi
  9. done

第二种方法:使用数组

  1. [root@db02 scripts]# cat 3.sh
  2. #!/bin/bash
  3. array=(I am oldboy teacher welcome to oldboy training class.)
  4. for ((i=0;i<${#array[@]};i++))
  5. do
  6. if [ "`echo ${array[i]}|wc -L`" -le 6 ]
  7. then
  8. echo ${array[i]}
  9. fi
  10. done

第三种方法:命令拼接

  1. echo "I am oldboy teacher welcome to oldboy training class." | awk '{for(i=1;i<=NF;i++){a=length($i);if(a <= 6){print $i}}}'

企业面试题11:

  开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。以屏幕输出的方式提醒用户比较结果。注意:一共是开发2个脚本。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。

  1. [root@db01 scripts]# cat read.sh
  2. #!/bin/sh
  3. #no.1
  4. read -p "Pls input two num:" a b
  5. [ -z "$a" -o -z "$b" ]&&{
  6. echo "must be two num."
  7. exit 1
  8. }
  9. #no.2
  10. expr $a + 1 &>/dev/null
  11. [ $? -ne 0 ]&&{
  12. echo "First arg must be int."
  13. exit 2
  14. }
  15. expr $b + 1 &>/dev/null
  16. [ $? -ne 0 ]&&{
  17. echo "Second arg must be int."
  18. exit 3
  19. }
  20. #no.3
  21. [ $a -gt $b ]&&{
  22. echo "$a > $b"
  23. exit 0
  24. }
  25. [ $a -eq $b ]&&{
  26. echo "$a = $b"
  27. exit 0
  28. }
  29. [ $a -lt $b ]&&{
  30. echo "$a < $b"
  31. exit 0
  32. }

企业面试题12:

打印选择菜单,一键安装Web服务:

  1. [root@oldboyscripts]# sh menu.sh
  2. 1.[install lamp]
  3. 2.[install lnmp]
  4. 3.[exit]
  5. pls input the num you want:

要求: 
1、当用户输入1时,输出“startinstalling lamp.”然后执行/server/scripts/lamp.sh,脚本内容输出”lampis installed“后退出脚本; 
2、当用户输入2时,输出“startinstalling lnmp.”然后执行/server/scripts/lnmp.sh输出”lnmpis installed“后退出脚本; 
3、当输入3时,退出当前菜单及脚本; 
4、当输入任何其它字符,给出提示“Input error”后退出脚本。 
5、要对执行的脚本进行相关条件判断,例如:脚本是否存在,是否可执行等。 
由10组黑哥提供(吴依) 
黑哥博客:www.dockerwy.com

  1. #!/bin/bash
  2. cat <<EOF
  3. 1.[install lamp]
  4. 2.[install lnmp]
  5. 3.[exit]
  6. pls input the num you want:
  7. EOF
  8. read -p "please input number 1-3:" a
  9. lamp() {
  10. if [ -f /server/scripts/lamp.sh ];then
  11. echo "startinstalling lamp."
  12. sleep 2
  13. sh /server/scripts/lamp.sh
  14. else
  15. echo "file does not exist"
  16. fi
  17. exit 0
  18. }
  19. lnmp () {
  20. if [ -f /server/scripts/lnmp.sh ];then
  21. echo "startinstalling lnmp."
  22. sleep 3
  23. sh /server/scripts/lnmp.sh
  24. else
  25. echo "file does not exist"
  26. fi
  27. exit 0
  28. }
  29. case $a in
  30. 1)
  31. lamp
  32. ;;
  33. 2)
  34. lnmp
  35. ;;
  36. 3)
  37. exit 0
  38. ;;
  39. *)
  40. echo "pls input the num you want:"
  41. ;;
  42. esac

企业面试题13:

1、监控web服务是否正常,不低于3种监控策略。 
2、监控db服务是否正常,不低于3种监控策略。 
要求间隔1分钟,持续监控。 
思路 
1、监控的种方式 
  通过进程名监控 ps -ef|grep name 
  通过端口监控 netstat -lntup|grep port/lsof -i:80 
  通过链接信息上的进程名 netstat -lntup|grep port 
2、等待一分钟 
3、后台执行 nohup

  1. #!/bin/bash
  2. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  3. check_httpd(){
  4. count=`ps -ef|grep nginx|grep -v grep|wc -l`
  5. if [ "$count" -ne 0 ]
  6. then
  7. action "nginx is ok" /bin/true
  8. else
  9. action "nginx is dead" /bin/false
  10. fi
  11. }
  12. check_httpd_port(){
  13. count=`netstat -lntup|grep 8080|wc -l`
  14. if [ "$count" -ne 0 ]
  15. then
  16. action "nginx is ok" /bin/true
  17. else
  18. action "nginx is dead" /bin/false
  19. fi
  20. }

企业面试题14:

  监控memcache服务是否正常,模拟用户(web客户端)检测。 
使用nc命令加上set/get来模拟检测,以及监控响应时间及命中率。 
感谢乔飞翔同学的分享

  1. [root@db02 scripts]# cat mem.sh
  2. #!/bin/bash
  3. ########################
  4. #date:2016-07-17 #
  5. #author:fxqiao #
  6. #QQ:827724746 #
  7. #E-mail:qfx1995@163.com#
  8. ########################
  9. #echo -ne "\033[0;33m"
  10. echo -ne "\E[1;32m"
  11. cat<<EOT
  12. _oo0oo_
  13. 088888880
  14. 88" . "88
  15. (| -_- |)
  16. 0\ = /0
  17. ___/'---'\___
  18. .' \\\\| |// '.
  19. / \\\\||| : |||// \\
  20. /_ ||||| -:- |||||- \\
  21. | | \\\\\\ - /// | |
  22. | \_| ''\---/'' |_/ |
  23. \ .-\__ '-' __/-. /
  24. ___'. .' /--.--\ '. .'___
  25. ."" '< '.___\_<|>_/___.' >' "".
  26. | | : '- \'.;'\ _ /';.'/ - ' : | |
  27. \ \ '_. \_ __\ /__ _/ .-' / /
  28. ====='-.____'.___ \_____/___.-'____.-'=====
  29. '=---='
  30. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. 佛祖保佑 iii 服务正常
  32. EOT
  33. echo -ne "\E[0m"
  34. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  35. ###variables
  36. RETVAL=0
  37. ###color
  38. RED_COLOR='\E[1;31m'
  39. GREEN_COLOR='\E[1;32m'
  40. YELLOW_COLOR='\E[1;33m'
  41. BLUE_COLOR='\E[1;34m'
  42. RES='\E[0m'
  43. ####menu list
  44. menu() {
  45. cat <<EOF
  46. ############################
  47. 1 memcached status
  48. 2 memcached stop
  49. 3 memcached get_hits
  50. 4 quit
  51. EOF
  52. }
  53. status() {
  54. Set=$(printf "set key1 0 0 6\r\noldboy\r\n"|nc 127.0.0.1 11211|grep STORED|wc -l)
  55. Get=$(printf "get key1\r\n"|nc 127.0.0.1 11211|grep oldboy|wc -l)
  56. Port=$(netstat -lntup|grep memcached|wc -l)
  57. if [ $Port -ge 4 ];then
  58. Num=$(expr $Set + $Get)
  59. if [ $Num -eq 2 ];then
  60. action "Memcached server is running" /bin/true
  61. else
  62. action "Memcached server is not normal" /bin/false
  63. fi
  64. else
  65. action "Memcached server is not running" /bin/false
  66. read -t 10 -p "Memcached server whether open:[y/n]:" b
  67. if [ "$b" == "y" -o "$b" == "Y" ];then
  68. /usr/bin/memcached -m 16m -p 11211 -d -u root -c 8192
  69. action "Memcached server is starting" /bin/true
  70. elif [ "$b" == "n" -o "$b" == "N" ];then
  71. echo -e "$YELLOW_COLOR Goodbye.............$RES"
  72. fi
  73. fi
  74. return $RETVAL
  75. }
  76. stop() {
  77. Port=$(netstat -lntup|grep memcached|wc -l)
  78. if [ $Port -ge 4 ];then
  79. pkill memcached &>/dev/null
  80. RETVAL=$?
  81. if [ $RETVAL -eq 0 ];then
  82. action "Memcached server is stopping" /bin/true
  83. else
  84. action "Memcached server is stopping" /bin/false
  85. fi
  86. else
  87. action "Memcached server not running" /bin/false
  88. fi
  89. }
  90. get_hits() {
  91. Cmd_get=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "cmd_get"|awk '{print $3}')
  92. Get_hits=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "get_hits"|awk '{print $3}')
  93. Get_misses=$(printf "stats\r\n"|nc 127.0.0.1 11211|grep "get_misses"|awk '{print $3}')
  94. Port=$(netstat -lntup|grep memcached|wc -l)
  95. if [ $Port -ge 4 ];then
  96. echo "A total of cmd_get is $Cmd_get"
  97. sleep 1
  98. echo "A total of get_hits is $Get_hits"
  99. sleep 1
  100. echo "A total of get_misses is $Get_misses"
  101. else
  102. action "Memcached is not running" /bin/false
  103. fi
  104. return $RETVAL
  105. }
  106. main() {
  107. while true
  108. do
  109. menu
  110. read -t 10 -p "Please input number:" a
  111. expr $a + 1 &>/dev/null
  112. if [ $? -ne 0 ];then
  113. echo -e "$BLUE_COLOR Please input error,not int $RES"
  114. exit 1
  115. fi
  116. case "$a" in
  117. 1)
  118. status
  119. RETVAL=$?
  120. ;;
  121. 2)
  122. stop
  123. RETVAL=$?
  124. ;;
  125. 3)
  126. get_hits
  127. RETVAL=$?
  128. ;;
  129. 4)
  130. clear
  131. echo "##############################################"
  132. echo -e "$GREEN_COLOR `figlet GoodBye` $RES"
  133. echo "##############################################"
  134. exit 0
  135. ;;
  136. *)
  137. echo -e "$RED_COLOR input error $RES"
  138. esac
  139. done
  140. }
  141. main
  142. exit $RETVAL

企业面试题15:

面试及实战考试题:监控web站点目录(/var/html/www)下所有文件是否被恶意篡改(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次(10分钟时间完成)。

  1. [root@db02 tmp]# cat html.sh
  2. #!/bin/sh
  3. html_dir=/var/html/www
  4. html_file=`find /var/html/www -type f`
  5. check_dir=/tmp/checkdir
  6. [ ! -d $check_dir ] && mkdir $check_dir
  7. for n in $html_file
  8. do
  9. md5sum $n >>$check_dir/1.txt
  10. done
  11. while true
  12. do
  13. md5sum -c $check_dir/1.txt|grep FAILED >>$check_dir/2.txt
  14. [ -s $check_dir/2.txt ] && \
  15. echo "`cat $check_dir/2.txt`"|mail -s "date:`date +%F-%H:%M:%S` Web is dangerous" 18576749166@163.com
  16. >$check_dir/2.txt
  17. sleep 3
  18. done

企业面试题16:

企业案例:写网络服务独立进程模式下rsync的系统启动脚本 
例如:/etc/init.d/rsyncd{start|stop|restart}。 
要求: 
1.要使用系统函数库技巧。 
2.要用函数,不能一坨SHI的方式。 
3.可被chkconfig管理。

  1. [root@www tmp]# cat 123.sh
  2. #!/bin/bash
  3. ##################
  4. #I am yuhongcong
  5. #qq:604419314
  6. #################
  7. # Comments to support chkconfig on RedHat Linux
  8. # chkconfig: 2345 64 36
  9. status1=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
  10. pidfile="/etc/rsync/rsyncd.pid"
  11. start_rsync="rsync --daemon --config=/etc/rsync/rsyncd.conf"
  12. function rsyncstart() {
  13. if [ "${status1}X" == "X" ];then
  14. rm -f $pidfile
  15. ${start_rsync}
  16. status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
  17. if [ "${status2}X" != "X" ];then
  18. echo "rsync service start.......OK"
  19. fi
  20. else
  21. echo "rsync service is running !"
  22. fi
  23. }
  24. function rsyncstop() {
  25. if [ "${status1}X" != "X" ];then
  26. kill -9 $(cat $pidfile)
  27. status2=$(ps -ef | egrep "rsync --daemon.*rsyncd.conf" | grep -v 'grep')
  28. if [ "${statusw2}X" == "X" ];then
  29. echo "rsync service stop.......OK"
  30. fi
  31. else
  32. echo "rsync service is not running !"
  33. fi
  34. }
  35. function rsyncstatus() {
  36. if [ "${status1}X" != "X" ];then
  37. echo "rsync service is running !"
  38. else
  39. echo "rsync service is not running !"
  40. fi
  41. }
  42. function rsyncrestart() {
  43. if [ "${status1}X" == "X" ];then
  44. echo "rsync service is not running..."
  45. rsyncstart
  46. else
  47. rsyncstop
  48. rsyncstart
  49. fi
  50. }
  51. case $1 in
  52. "start")
  53. rsyncstart
  54. ;;
  55. "stop")
  56. rsyncstop
  57. ;;
  58. "status")
  59. rsyncstatus
  60. ;;
  61. "restart")
  62. rsyncrestart
  63. ;;
  64. *)
  65. echo
  66. echo "Usage: $0 start|stop|restart|status"
  67. echo
  68. esac

企业面试题17:

老男孩教育天津项目学生实践抓阄题目: 
  好消息,老男孩培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。 
因此需要挑选学生,因此需要一个抓阄的程序: 
要求: 
  1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。 
  2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入。

  1. [root@db02 tmp]# cat select17.sh
  2. #!/bin/sh
  3. > /tmp/temp.txt
  4. input(){
  5. while true
  6. do
  7. read -p "pls input your name:" name
  8. if [ -z $name ]
  9. then
  10. continue
  11. elif [ $name == "0" ]
  12. then
  13. break
  14. fi
  15. rand=$((RANDOM%100))
  16. echo -e $rand"\t"$name >>/tmp/temp.txt
  17. done
  18. }
  19. output(){
  20. cat /tmp/temp.txt |sort -n -k1 -r|sed '3a#################'
  21. }
  22. main(){
  23. input
  24. output
  25. }
  26. main

企业面试题18:

  已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?

21029299
00205d1c
a3da1677
1f6d12dd
890684b

方法一:

  1. #!/bin/bash
  2. . /etc/init.d/functions
  3. MD5PASS=(
  4. 21029299
  5. 00205d1c
  6. a3da1677
  7. 1f6d12dd
  8. 890684b
  9. )
  10. for ((n=0;n<=32767;n++))
  11. do
  12. for((i=0;i<${#MD5PASS[*]};i++))
  13. do
  14. md5=`echo $n | md5sum|cut -c 1-8`
  15. if [ "$md5" == ${MD5PASS[$i]} ]
  16. then
  17. echo "$n" "${MD5PASS[$i]} "
  18. fi
  19. done
  20. done

方法二:

  1. [root@openvpn-server ~]# cat md5sum.sh
  2. #!/bin/sh
  3. a=(
  4. 21029299
  5. 00205d1c
  6. a3da1677
  7. 1f6d12dd
  8. 890684b
  9. )
  10. for i in `seq 32767`
  11. do
  12. source=`echo $i|md5sum|cut -c 1-8`
  13. for j in ${a[@]}
  14. do
  15. if [ $source == $j ];then
  16. echo $source "-->" $i
  17. fi
  18. done
  19. done

方法三:

  1. [root@m01 ~]# cat mianshiti6.sh
  2. #!/bin/bash
  3. mima=(
  4. 21029299
  5. 00205d1c
  6. a3da1677
  7. 1f6d12dd
  8. 890684b
  9. )
  10. for i in {1..32767}
  11. do
  12. num=`echo $i | md5sum | cut -c -8`
  13. for n in ${mima[*]}
  14. do
  15. if [ "$num" == "$n" ]
  16. then
  17. echo "$i match $n"
  18. continue
  19. fi
  20. done
  21. done

企业面试题19:

批量检查多个网站地址是否正常 
要求: shell数组方法实现,检测策略尽量模拟用户访问思路 
http://www.etiantian.org 
http://www.taobao.com 
http://oldboy.blog.51cto.com 
http://10.0.0.7

  1. [root@db02 scripts]# cat aaa.sh
  2. #!/bin/sh
  3. ################
  4. #Author:YuHongCong
  5. #604419314@qq.com
  6. ################
  7. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  8. URLS=(
  9. http://www.etiantian.org
  10. http://www.taobao.com
  11. http://oldboy.blog.51cto.com
  12. http://10.0.0.70
  13. )
  14. CHECK_URL(){
  15. wget -T 3 --spider --tries=2 $1 &>/dev/null
  16. if [ $? -eq 0 ]
  17. then
  18. return 0
  19. else
  20. return 1
  21. fi
  22. }
  23. MON_URL(){
  24. for url in ${URLS[*]}
  25. do
  26. CHECK_URL $url
  27. if [ $? -eq 0 ]
  28. then
  29. action "$url" /bin/true
  30. else
  31. action "$url" /bin/false
  32. fi
  33. done
  34. }
  35. main(){
  36. while true
  37. do
  38. MON_URL
  39. sleep 10
  40. done
  41. }
  42. main


老男孩推荐:(包含倒计时的方法)

  1. [root@db01 shizhan]# cat oldboy01.sh
  2. #!/bin/sh
  3. ################
  4. #Author:oldboy
  5. #31333741@qq.com
  6. ################
  7. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  8. URLS=(
  9. http://www.etiantian.org
  10. http://www.taobao.com
  11. http://oldboy.blog.51cto.com
  12. http://10.0.0.70
  13. )
  14. LTIME(){
  15. echo -n "time;"
  16. for n in {1..10}
  17. do
  18. if [ $n -eq 10 ]
  19. then
  20. echo "start"
  21. else
  22. echo -n .
  23. fi
  24. sleep 1
  25. done
  26. }
  27. CHECK_URL(){
  28. wget -T 3 --spider --tries=2 $1 &>/dev/null
  29. if [ $? -eq 0 ]
  30. then
  31. return 0
  32. else
  33. return 1
  34. fi
  35. }
  36. MON_URL(){
  37. for url in ${URLS[*]}
  38. do
  39. CHECK_URL $url
  40. if [ $? -eq 0 ]
  41. then
  42. action "$url" /bin/true
  43. else
  44. action "$url" /bin/false
  45. fi
  46. done
  47. }
  48. main(){
  49. while true
  50. do
  51. LTIME
  52. MON_URL
  53. sleep 10
  54. done
  55. }
  56. main

企业面试题20(中企动力):

用shell处理以下内容 
1、按单词出现频率降序排序! 
2、按字母出现频率降序排序!

the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation
  1. #!/bin/sh
  2. str="the squid project provides a number of resources toassist users design,implement and support squid installations. Please browsethe documentation and support sections for more infomation"
  3. words(){
  4. echo $str|sed 's#[^a-zA-Z]# #g'|tr " " "\n"|grep -v "^$"|sort|uniq -c|sort -r -n
  5. }
  6. letters(){
  7. echo $str|grep -o "."|sort|egrep -v " |^$|[^a-zA-Z]"|uniq -c|sort -n -r
  8. }
  9. case $1 in
  10. words)
  11. words
  12. ;;
  13. letters)
  14. letters
  15. ;;
  16. *)
  17. echo "usage:$0 {words|letters}"
  18. esac

企业面试题22:

  开发通过web界面展示监控Nginx代理节点状态,效果图如下。 

文件打包 密码:xso0 
脚本及内容请下载 

lvs+keepalived集群部分Shell企业案例:

企业面试题23、

【LVS主节点】手工开发ipvsadm管理lvs的脚本ip_vs

实现:/etc/init.d/lvs {start|stop|restart}

  1. [root@www tmp]# cat lvs.sh
  2. #!/bin/bash
  3. if [ $UID -ne 0 ];then
  4. echo "Permission denied (you must be root)"
  5. exit 1
  6. fi
  7. [ -f /etc/init.d/functions ] && . /etc/init.d/functions
  8. vip_netmask=10.0.0.3/24
  9. vip=10.0.0.3
  10. service_addr=10.0.0.3:80
  11. rs=(
  12. 10.0.0.7:80
  13. 10.0.0.8:80
  14. )
  15. start() {
  16. #add vip
  17. ifconfig|grep $vip &>/dev/null
  18. if [ $? -ne 0 ];then
  19. ip addr add $vip_netmask dev eth0 label eth0:0 && \
  20. action "add vip $vip_netmask" /bin/true
  21. else
  22. echo "vip $vip_netmask already exists."
  23. fi
  24. lvs_table=$(ipvsadm -ln|grep "$vip"|wc -l)
  25. if [ $lvs_table -eq 1 ];then
  26. echo "virtual server already exists."
  27. else
  28. #add virtual server
  29. ipvsadm -A -t $service_addr -s wrr && \
  30. action "add virtual server $service_addr" /bin/true
  31. fi
  32. for ip in ${rs[@]};do
  33. rs_num=$(ipvsadm -ln|grep "$ip"|wc -l)
  34. if [ $rs_num -eq 1 ];then
  35. echo "real server $ip already exists."
  36. else
  37. #add real server
  38. ipvsadm -a -t $service_addr -r $ip -g -w 1 && \
  39. action "add real server $ip" /bin/true
  40. fi
  41. done
  42. #set tcp tcpfin udp connection timeout
  43. ipvsadm --set 30 5 60 && \
  44. action "set tcp tcpfin udp connection timeout values." /bin/true
  45. }
  46. stop() {
  47. ifconfig|grep $vip &>/dev/null
  48. if [ $? -ne 0 ];then
  49. echo "without vip $vip"
  50. else
  51. #delete vip
  52. ip addr del $vip_netmask dev eth0 label eth0:0 && \
  53. action "delete vip $vip_netmask." /bin/true
  54. fi
  55. lvs_table=$(ipvsadm -ln|grep "$vip"|wc -l)
  56. for ip in ${rs[@]};do
  57. rs_num=$(ipvsadm -ln|grep "$ip"|wc -l)
  58. let lvs_table+=rs_num
  59. done
  60. if [ $lvs_table -ge 1 ];then
  61. #clear all table
  62. ipvsadm -C && \
  63. action "clear all lvs table." /bin/true
  64. else
  65. echo "lvs table is empty."
  66. fi
  67. }
  68. case "$1" in
  69. start)
  70. start
  71. ;;
  72. stop)
  73. stop
  74. ;;
  75. restart)
  76. stop
  77. sleep 2
  78. start
  79. ;;
  80. *)
  81. echo "USAGE: $0 {start|stop|restart}"
  82. esac

企业面试题24

  【LVS主节点】模拟keepalived健康检查功能管理LVS节点, 
当节点挂掉(检测2次,间隔2秒)从服务器池中剔除,好了(检测2次,间隔2秒)加进来 
提示:利用ipvsadm命令实现添加和减少LVS节点。

  1. [root@www tmp]# cat lvs_check_rs.sh
  2. #!/bin/bash
  3. vip=10.0.0.3
  4. rs_ip=(
  5. 10.0.0.8
  6. 10.0.0.7
  7. )
  8. remove_check() {
  9. for rs in ${rs_ip[@]};do
  10. ipvsadm -ln|grep $rs &>/dev/null
  11. if [ $? -eq 0 ];then
  12. rs_donw=0
  13. a_group_down=0
  14. for i in 1 2;do
  15. rs_down=$(nmap -p 80 $rs|grep open|wc -l)
  16. let a_group_down+=rs_down
  17. sleep 2
  18. done
  19. if [ $a_group_down -eq 0 ];then
  20. ipvsadm -d -t ${vip}:80 -r ${rs}:80
  21. fi
  22. fi
  23. done
  24. }
  25. join_check() {
  26. for rs in ${rs_ip[@]};do
  27. ipvsadm -ln|grep $rs &>/dev/null
  28. if [ $? -ne 0 ];then
  29. a_group_up=0
  30. rs_up=0
  31. for j in 1 2;do
  32. rs_up=$(nmap -p 80 $rs|grep open|wc -l)
  33. let a_group_up+=rs_up
  34. sleep 2
  35. done
  36. if [ $a_group_up -eq 2 ];then
  37. ipvsadm -a -t ${vip}:80 -r ${rs}:80 -g -w 1
  38. fi
  39. fi
  40. done
  41. }
  42. check_rs() {
  43. while true;do
  44. remove_check
  45. sleep 1
  46. join_check
  47. sleep 1
  48. done
  49. }
  50. check_rs

企业面试题25

【LVS客户端节点】开发LVS客户端设置VIP以及抑制ARP的管理脚本 
实现:/etc/init.d/lvsclient {start|stop|restart}

  1. [root@web01-lnmp-09 scripts]# cat /etc/init.d/lvsclient
  2. #!/bin/bash
  3. #chkconfig: 2345 37 57
  4. #Write by Bevin 2016
  5. #LVS_Nginx
  6. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  7. [ -f /etc/init.d/functions ] && source /etc/init.d/functions
  8. #Step 1
  9. function L_ipadd() {
  10. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  11. if [ $VIPRES -eq 0 ]
  12. then
  13. ip addr add 10.0.0.12/32 dev lo label lo:12
  14. route add -host 10.0.0.12 dev lo
  15. sleep 1
  16. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  17. if [ $VIPRES -eq 0 ]
  18. then
  19. ip addr add 10.0.0.12/32 dev lo label lo:12
  20. route add -host 10.0.0.12 dev lo
  21. sleep 1
  22. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  23. if [ $VIPRES -eq 0 ]
  24. then
  25. action "ip addr add 10.0.0.12/32 ..." /bin/false
  26. exit 1
  27. else
  28. action "ip addr add 10.0.0.12/32 ..." /bin/true
  29. fi
  30. else
  31. action "ip addr add 10.0.0.12/32 ..." /bin/true
  32. fi
  33. else
  34. action "ip addr add 10.0.0.12/32 is exist."
  35. fi
  36. echo "1" > /proc/sys/net/ipv4/conf/lo/arp_ignore
  37. echo "2" > /proc/sys/net/ipv4/conf/lo/arp_announce
  38. echo "1" > /proc/sys/net/ipv4/conf/all/arp_ignore
  39. echo "2" > /proc/sys/net/ipv4/conf/all/arp_announce
  40. }
  41. function L_ipdel() {
  42. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  43. if [ $VIPRES -ne 0 ]
  44. then
  45. ip addr del 10.0.0.12/32 dev lo
  46. route del -host 10.0.0.12 dev lo
  47. sleep 1
  48. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  49. if [ $VIPRES -ne 0 ]
  50. then
  51. action "ip addr del 10.0.0.12/32 ..." /bin/false
  52. exit 1
  53. else
  54. action "ip addr del 10.0.0.12/32 ..." /bin/true
  55. fi
  56. else
  57. action "ip addr del 10.0.0.12/32 ..." /bin/true
  58. fi
  59. echo "0" > /proc/sys/net/ipv4/conf/lo/arp_ignore
  60. echo "0" > /proc/sys/net/ipv4/conf/lo/arp_announce
  61. echo "0" > /proc/sys/net/ipv4/conf/all/arp_ignore
  62. echo "0" > /proc/sys/net/ipv4/conf/all/arp_announce
  63. }
  64. #Step 2
  65. function L_exec() {
  66. case "$1" in
  67. start)
  68. L_ipadd
  69. ;;
  70. stop)
  71. L_ipdel
  72. ;;
  73. status)
  74. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  75. sleep 1
  76. if [ $VIPRES -ne 0 ]
  77. then
  78. echo "LVS Nginx is working"
  79. else
  80. echo "LVS Nginx is not working"
  81. fi
  82. ;;
  83. restart)
  84. VIPRES=`ip addr|grep 10.0.0.12|wc -l`
  85. if [ $VIPRES -ne 0 ]
  86. then
  87. L_ipdel
  88. sleep 1
  89. L_ipadd
  90. else
  91. echo "ip 10.0.0.12 is not working"
  92. sleep 1
  93. L_ipadd
  94. fi
  95. ;;
  96. *)
  97. echo "USAGE: $0 {start|stop|restart|status}"
  98. esac
  99. }

企业面试题26

【LVS备节点】模拟keepalved vrrp功能,监听节点,如果主节点不可访问则备节点启动并配置LVS实现接管主节点的资源提供服务(提醒:注意ARP缓存)

  1. [root@lvs-lb01-05 scripts]# cat lvs.sh
  2. #!/bin/bash
  3. #Write by Bevin 2016
  4. export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
  5. [ -f /etc/init.d/functions ] && source /etc/init.d/functions
  6. #Step 1 Instal ip_vs
  7. Ch_ipvs=`rpm -qa ipvsadm|wc -l`
  8. if [ $Ch_ipvs -eq 0 ]
  9. then
  10. yum install ipvsadm -y
  11. fi
  12. #Step 2 Functions
  13. VIP=10.0.0.12
  14. V_TAIL=12
  15. RIP=(
  16. 9
  17. 10
  18. )
  19. function L_LVS_VIP_RIP() {
  20. VIPRES=`ip addr|grep $VIP|wc -l`
  21. if [ $VIPRES -eq 0 ]
  22. then
  23. ip addr add $VIP/24 dev eth0 label eth0:$V_TAIL
  24. ipvsadm -C
  25. ipvsadm -A -t $VIP:80 -s rr
  26. for n in ${RIP[*]}
  27. do
  28. ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -g
  29. done
  30. else
  31. ipvsadm -C
  32. ipvsadm -A -t $VIP:80 -s rr
  33. for n in ${RIP[*]}
  34. do
  35. ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80 -g
  36. done
  37. fi
  38. }
  39. function L_check_vip() {
  40. VIPRES=`ip addr|grep $VIP|wc -l`
  41. if [ $VIPRES -eq 0 ]
  42. then
  43. L_LVS_VIP_RIP
  44. sleep 5
  45. if [ $VIPRES -eq 0 ]
  46. then
  47. L_LVS_VIP_RIP
  48. sleep 5
  49. if [ $VIPRES -eq 0 ]
  50. then
  51. echo "Warning: lb01: $VIP is down."|mail -s txbevin@sina.com
  52. exit 1
  53. fi
  54. fi
  55. fi
  56. sleep 5
  57. }
  58. function L_check_rip() {
  59. for n in ${RIP[*]}
  60. do
  61. CurlRes=`curl -I -s 10.0.0.$n|grep -E "200|301|302"|wc -l`
  62. if [ $CurlRes -ne 0 ]
  63. then
  64. RS=`ipvsadm -Ln|grep 10.0.0.$n|wc -l`
  65. if [ $RS -eq 0 ]
  66. then
  67. ipvsadm -a -t $VIP:80 -r 10.0.0.$n:80
  68. fi
  69. else
  70. ipvsadm -d -t $VIP:80 -r 10.0.0.$n:80
  71. mail -s "10.0.0.$n nginx is down at $(date +%F-%T)." txbevin@sina.com
  72. continue
  73. fi
  74. done
  75. sleep 5
  76. }
  77. #Step 3
  78. L_LVS_VIP_RIP
  79. while true
  80. do
  81. L_check_vip
  82. sleep 1
  83. L_check_rip
  84. sleep 1
  85. done

后面几道题如果大家会可以把答案发送给我 
     mail:new_oldboy@163.com 
-------------------

企业面试题27

  请用shellPython编写一个正方形(oldboy_square.sh),接收用户输入的数字。 
例如:

  1. [root@oldboy ~]# sh oldboy_square1.sh
  2. Please Enter a number:5
  3. ++++++++++
  4. ++++++++++
  5. ++++++++++
  6. ++++++++++
  7. ++++++++++
  8. [root@oldboy ~]# sh oldboy_square2.sh
  9. Please Enter a number:9
  10. ■■■■■■■■■
  11. ■■■■■■■■■
  12. ■■■■■■■■■
  13. ■■■■■■■■■
  14. ■■■■■■■■■
  15. ■■■■■■■■■
  16. ■■■■■■■■■
  17. ■■■■■■■■■
  18. ■■■■■■■■■

企业面试题28

  请用shellPython编写一个等腰三角形(oldboy2_triangle.sh),接收用户输入的数字。 
例如:

  1. [root@oldboy ~]# sh oldboy2_triangle.sh
  2. Please Enter a number:5
  3. *
  4. ***
  5. *****
  6. *******
  7. *********
  8. [root@oldboy ~]# sh oldboy2_triangle.sh
  9. Please Enter a number:8
  10. *
  11. ***
  12. *****
  13. *******
  14. *********
  15. ***********
  16. *************
  17. ***************

企业面试题29

  请用shellPython编写一个画直角梯形程序(oldboy4.sh),接收用户输入的参数n,m 
例如:

  1. [root@oldboy ~]# sh oldboy4.sh 4 6
  2. ****
  3. *****
  4. ******

27,28,29三道题可以参考 
http://oldboy.blog.51cto.com/2561410/1718607



本文出自 “老男孩linux培训” 
请务必保留此出处http://oldboy.blog.51cto.com/2561410/1632876

老男孩Shell企业面试题30道 [答案]相关推荐

  1. shell企业面试题

    shell企业面试题 1.利用bash for循环打印下面这句话中字母数不大于6的单词 I am oldboy teacher welcome to oldboy training class [ro ...

  2. python企业面试题250道

    Python基础 文件操作 ◾1.有一个jsonline格式的文件file.txt大小约为10K ◾2.补充缺失的代码 模块与包 ◾3.输入日期, 判断这一天是这一年的第几天? ◾4.打乱一个排好序的 ...

  3. 前端发送的字符串有大小限制吗_前端经典面试题 30道

    30 道前端面试题包含 JS.CSS.React.网络.浏览器.程序题等出处:https://segmentfault.com/a/1190000020391424 题 1 :什么是防抖和节流?有什么 ...

  4. 2021年MyBatis面试题30道

    文章目录 前言 面试题系列文章传送门 MyBatis面试题内容 1. 模糊查询like语句该怎么写? 2. MyBatis 框架适用场合? 3. MyBatis是如何进行分页的?分页插件的原理是什么? ...

  5. shell企业面试题练习

    问题1:使用Linux命令查询file1中空行所在的行号 #!/bin/bash awk '/^$/ {print NR}' file1 问题2:有文件chengji.txt内容如下'' 张三 40 ...

  6. c++常见面试题30道

    1.new.delete.malloc.free关系 delete会调用对象的析构函数,和new对应free只会释放内存,new调用构造函数.malloc与free是C++/C语言的标准库函数,new ...

  7. C++常见面试题-30道

    来自:http://blog.csdn.net/wangshihui512/article/details/9092439 1.new.delete.malloc.free关系 delete会调用对象 ...

  8. 吊打面试官之SQL面试题30问及答案

    经典SQL30问一: 编写查询,查找表中的行总数. 编写查询,消除表结果中的重复记录. 编写查询,获取t_employee表中designation字段前3个字符. 查询t_employee表,合并输 ...

  9. 前端经典面试题 30道

    题 1 :什么是防抖和节流?有什么区别?如何实现? 防抖:触发高频事件后n秒内函数只会执行一次,如果n秒内高频事件再次被触发,则重新计算时间 思路:每次触发事件时都取消之前的延时调用方法 节流:高频事 ...

最新文章

  1. Hadoop详解(一):Hadoop简介
  2. SQLServer导入Excel截断数据的解决办法
  3. 史上最严重的忘拿钥匙事件 | 今日最佳
  4. 三、Numpy数组操作
  5. bufferedreader读取中文乱码_python之pandas模块关于csv文件乱码问题解决
  6. Flutter实战一Flutter聊天应用(一)
  7. 百度网页搜索无法通过域名访问_网站换域名或网页内容改版对网站的影响以及网站换域名注意事项...
  8. python dlib opencv人脸识别准确度_基于dlib和opencv库的人脸识别
  9. spring事物配置
  10. angular 点菜_JavaScript实现的select点菜功能示例
  11. UNIX文件系统概述
  12. android webview 文件下载,Android编程使用WebView实现文件下载功能的两种方法
  13. iOS webp图片展示处理
  14. linux进程中monitor,linux进程监控,monitor脚本
  15. oracle版本区别 win7_Oracle在Win7服务管理中消失的解决方法
  16. Windows系统桌面快捷方式图标去除小箭头
  17. 【电力电子技术】整流电路
  18. 【系统分析师之路】2008年上系统分析师上午综合知识真题
  19. 二极管结电容和反向恢复时间都是怎么来的
  20. pg_receivewal实践

热门文章

  1. mapdb java_MapDB使用入门
  2. Jupyter 快速入门
  3. oracle格式化列宽度,ORACLE日期时间的格式化参数大全
  4. mysql 复杂的sql_mysql 一个复杂的sql
  5. java的数列极差_[hoj]数列极差问题 | 学步园
  6. 事物日志恢复 mysql_浅谈SQL Server中的事务日志(五)----日志在高可用和灾难恢复中的作用...
  7. python矩阵中找满足条件的元素_Python 找到列表中满足某些条件的元素方法
  8. 【测试工具】在linux测试环境安装bug管理工具禅道
  9. B1023 组个最小数 (20分)
  10. 定义一个类:实现功能可以返回随机的10个数字,随机的10个字母, 随机的10个字母和数字的组合;字母和数字的范围可以指定,类似(1~100)(A~z)...