20道shell编程面试题-下

企业面试题13:

1、监控web服务是否正常,不低于3种监控策略.

2、监控db服务是否正常,不低于3种监控策略.

3、要求间隔1分钟,持续监控.

1、监控web服务是否正常,不低于3种监控策略.
2、监控db服务是否正常,不低于3种监控策略.
3、要求间隔1分钟,持续监控.
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
usage(){
   echo "USAGE:$0 url"
   exit 1
}

CheckUrl(){
    # curl -I $1 2>/dev/null
    # netstat -lntup|grep 80
    # ps -ef | grep -v grep | grep nginx
    # lsof -i:80
    wget -T 10 --spider -t 2 $1 &>/dev/null
	RETVAL=$?
	if [ $RETVAL -eq 0 ];then
	    action "$1 url" /bin/true
	else
	    action "$1 url" /bin/false
	fi
	return $RETVAL
}
main(){
    if [ $# -ne 1 ];then
	    usage
	fi
    CheckUrl $1
}
main $*

企业面试题14:

监控web站点目录(/var/html/www)下所有文件是否被恶意篡改,(文件内容被改了),如果有就打印改动的文件名(发邮件),定时任务每3分钟执行一次.

#!/bin/bash
path=/var/html/www
[ -d /tmp/file_md5 ] || mkdir -p /tmp/file_md5
md5_file=/tmp/file_md5/file_md5.log
total_file=/tmp/file_md5/file.log
right_num=$(cat $total_file | wc -l)
check_file_res=/tmp/file_md5/check_file.log
while true
do
  log=/tmp/file_md5/check_res.log
  [ -f $log ]||touch $log
  check_md5=$(md5sum -c $md5_file 2>/dev/null |grep FAILED|wc -l )
  check_file_num=$(find $path -type f | wc -l)
  find $path -type f > $check_file_res

  if [ $check_md5 -ne 0 ] || [ $check_file_num -ne $right_num ];then
    echo "$(md5sum -c $md5_file 2>/dev/null|grep FAILED)" > $log
    diff $total_file $check_file_res >> $log
    # mail -s "web site is misrepresented in $(date +F\ %T)" [email protected] < $log
    cat < $log
  else
    echo ok
  fi
  sleep 180
done
注释:
$log:如果文件被改动了,用于记录改动结果,发邮件时内容就是它;
$md5_file:保存着所有文件的md5值和文件路径,不能为空;
$total_file:保存着所有文件的绝对路径,不能为空;
$check_file_res:查找目录下的文件,将所有文件的绝对路径写入该文件,用来和$total_file比较不同.

企业面试题15:编写网络服务独立进程模式下rsync的系统启动脚本

例如:/etc/init.d/rsyncd{start|stop|restart}

要求:

1.要使用系统函数库技巧;

2.要用函数,不能是一坨的方式;

3.可被chkconfig管理.

#!/bin/bash
# chkconfig: 2345 32 62
# description: A rsync soft
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
pidfile='/var/run/rsyncd.pid'
judge(){
  RETVAL=$?
  if [ $RETVAL -eq 0 ];then
    action "rsync is $1" /bin/true
  else
    action "rsync is $1" /bin/false
  fi
  return $RETVAL
}

start(){
  if [ -f $pidfile ];then
    echo "rsync is running..."
    RETVAL=$?
  else
    rsync --daemon
    judge started
  fi
  return $RETVAL
}
stop(){
  if [ -f $pidfile ];then
    kill -USR2 $(cat $pidfile)
    rm -f $pidfile
    judge stopped
  else
    echo "rsync is already stopped"
    RETVAL=$?
  fi
  return $RETVAL
}

case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  restart)
    stop
    sleep 2
    start
    ;;
  *)
    echo "USAGE:$0 {start|stop|restart}"
    exit 1
esac
exit $RETVAL

企业面试题16:学生实践抓阄题目

1.执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就越有机会去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字;

2.第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入.

#!/bin/bash
while true
do
  file=/tmp/zhuajiu.txt
  [ -f $file ]||touch $file
  read -p "Pls input your English name: " name
  if [ -n $name ];then
    while true
    do
      flag=0
      luck_num=$(expr $RANDOM % 99 + 1)
      file_num=$(grep "\b${luck_num}\b" $file | wc -l)
      if [ $file_num -ne 1 ];then
        echo -e "$name \t $luck_num"
        echo -e "$name \t $luck_num" >> $file
        flag=1
      fi
      # 如果数字不重复,就会:输入一个名字,break跳出当前循环,让下个人输入名字
      [ $flag -eq 1 ] && break
    done
  else
    continue
  fi
done
注意事项:
A.对99取余再加1,就会得到一个1-99之间的随机数字;
B.脚本if中的while循环,对重复数字进行了判断:如果重复了,不用重新输入姓名,直接再生成随机数.

企业面试题17:

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

1d4171a9
599ae12e
5ce3771b

#!/bin/bash
count=0
for i in {1..32767}
do
MD5=`echo $i|md5sum|cut -c 1-8`
  if [ "$MD5" = "$1" -o "$MD5" = "$2" -o "$MD5" = "$3" ]
  then
    let count++
    echo "$i md5 is $MD5"
  fi
while [ $count -eq 3 ]
do
  exit
done
done

sh reverseMd5.sh 1d4171a9 599ae12e 5ce3771b
1233 md5 is 1d4171a9
2233 md5 is 599ae12e
3244 md5 is 5ce3771b
用元组的方式实现:
#!/bin/bash
array=(1d4171a9 599ae12e 5ce3771b)
count=0

for i in {1..32767}
do
MD5=`echo $i|md5sum|cut -c 1-8`
  if [ "$MD5" = "${array[0]}" -o "$MD5" = "${array[1]}" -o "$MD5" = "${array[2]}" ]
  then
    let count++
    echo "$i md5 is $MD5"
  fi
while [ $count -eq 3 ]
do
  exit
done
done

这里存在一种情况:给的随机数其实是有顺序的,要是if中没有或的判断,并且给随机数的时候不是按顺序给的,那就会大的先出来,小的出不来.

企业面试题18:批量检查多个网站地址是否正常

要求:shell数组方法实现,检测策略尽量模拟用户访问思路

http://www.baidu.com
http://www.taobao.com
http://oldboy.blog.51cto.com
# 传说中的wait函数
wait(){
echo -n "wait 3s"
for((i=0;i<3;i++))
do
  echo -n "."
  sleep 1
done
echo
}
脚本从这里开始
#!/bin/bash
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
array=(
http://www.baidu.com
http://www.taobao.com
http://oldboy.blog.51cto.com
)
# curl的-m参数--max-time
for i in ${array[*]}
do
  curl_cmd=`curl -I -m 3 $i 2>/dev/null|head -1|egrep '200|301|302'|wc -l`
  if [ $curl_cmd -eq 1 ]
  then
    action "$i url" /bin/true
  else
    action "$i url" /bin/false
  fi
done
# 打开后两个网站的速度比较慢,因为加载的静态文件太多,不碍事
http://www.baidu.com url         [  OK  ]
http://www.taobao.com url        [FAILED]
http://oldboy.blog.51cto.com url [FAILED]

企业面试题19:用shell处理以下内容

1.按单词出现频率降序排序!

2.按字母出现频率降序排序!

cat oldboy.txt # 这个文件其实是一行,我嫌一行太长了,就把它弄成了四行
The months of learning in Old Boy education are the few months that I think the time efficient is the most.
I had also studied at other training institutions before, 
but I was hard to understand what the tutor said and hard to follow. 
It was just too much to learn with no outline.

下面这几种方法的知识点含金量很高,值得多次品味

问题一

方法1:注意事项-要替换一个以上的字符时,用[]包起来;空格转换为换行符用tr

sed 's#[,\.]##g' oldboy.txt|tr " " "\n"|sort|uniq -c|sort -rn|head -5

方法2:

tr " ,\." "\n" <oldboy.txt|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn

方法3:直接用awk数组横向处理,而不是将单词竖向排列再处理

awk -F "[ ,.]+" '{for(i=1;i<NF;i++)S[$i]++}END{for(k in S) print S[k],k}' oldboy.txt |sort -rn|head -5

问题二

方法1:去空格特殊字符后,然后利用grep的-o将字符竖向排列后处理.-o表示只输出匹配的选项

sed 's#[,. ]##g' oldboy.txt|grep -o "."|sort|uniq -c|sort -rn|head -5

方法2:awk数组法

sed 's#[,. ]##g' oldboy.txt|grep -o "."|awk '{S[$1]++}END{for(k in S) print S[k],k}'|sort -rn|head -5

方法3:依然是直接用awk数组横向处理

sed 's#[,. ]##g' oldboy.txt|awk -F "" '{for(i=1;i<NF;i++)S[$i]++}END{for(k in S) print S[k],k}'|sort -rn

企业面试题20:输出正方形、等腰三角形、直角梯形

三角形:
#!/bin/bash
read -p "Pls input num: " n
for ((i=1;i<=$n;i++))
do
    for ((j=(($n-$i));j>0;j--))
    do
      echo -n " "
    done
    for ((m=0;m<$((2*$i-1));m++))
    do
      echo -n "*"
    done
    echo
done
正方形:
#!/bin/bash
read -p "Pls input num: " n
for ((i=1;i<=$n;i++))
do
    for ((j=1;j<=$n;j++))
    do
      echo -n "*"
    done
    echo
done
直角梯形:
#!/bin/bash
read -p "Pls input two num: " n m
a=$m-$n
b=$n
for ((i=1;i<$a;i++))
do
  c=$((b++))
  for ((j=1;j<=$c;j++))
  do
    echo -n "*"
  done
  echo
done
for ((j=1;j<=$m;j++))
do
  echo -n "*"
done
echo

注意下面这段代码:
#!/bin/bash
a=3
for ((i=0;i<4;i++))
do
  c=$((a++))
  echo $a
  echo $c
done
这种累加--a每次加1,第一次的结果是a为4,但结果并不给c,把还没累加过的a赋值给c,第一次的结果是3.

猜你喜欢

转载自www.cnblogs.com/fawaikuangtu123/p/10905235.html
今日推荐