Shell面试题

2,315 阅读4分钟
原文链接: www.cnblogs.com

目录

 


11-12 Shell练习题

回到顶部

1、中企动力面试题

用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
#!/usr/bin/bash
##############################################################
# File Name: tongji.sh
# Version: V1.0
# Author: CentOS
# Organization: http://47.94.88.182/
# Created Time : 2018-11-12 11:13:17
# Description:
##############################################################

zimu=`echo 'the squid project provides a number of resources toassist users design,implement and sup
port squid installations. Please browsethe documentation and support sections for more infomation'|sed -r 's#[.,]# #g'`
echo $zimu|xargs -n1|awk '{array[$1]++}END{for(j in array)print j,array[j]}'|sort -k2 -rn|column -t

echo $zimu|egrep -o "[a-Z]"|sort|uniq -c |sort -nr |column -t
回到顶部

2、企业面试题

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

21029299
00205d1c
a3da1677
1f6d12dd

解答:利用random的范围(0-32767)

#!/bin/bash
a=(21029299
00205d1c
a3da1677
1f6d12dd)
for n in {0..32767}
do
        random=`echo $n|md5sum|cut -c1-8`
        for((i=0;i<=${#a[@]};i++))
        do
          if [ "$random" == "${a[i]}" ];then
          echo "$n" "${a[i]}"
        fi
        done
done
回到顶部

3、使用for循环在/oldboy目录下批量创建10个html文件

通过随机小写10个字母加固定字符串oldboy批量创建10个html文件

mkdir /oldboy && cd /oldboy && for i in `seq 10`;do a=`echo $RANDOM|md5sum|tr "0-9" "j-z"|cut -c1-10`;touch ${a}_oldboy.html;done
回到顶部

4、请用至少两种方法实现!

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

rename的方法

cd /oldboy && rename _oldboy.html _oldgirl.HTML *

sed命令拼接

cd /oldboy/ && ls  |sed -r 's#(.*)_(.*)#mv \1_\2 \1_oldgirl.HTML#'|bash

for循环

cd /oldboy
for i in `find /oldboy -type f -name  "*_oldboy.html"`
do
b=`echo $i|cut -c 1-19`
mv ${b}oldboy.html ${b}oldgirl.HTML
done
cd /oldboy
for i in `find /oldboy -type f -name  "*_oldboy.html"`
do
b=`echo $i |sed -r 's#(.*)oldboy.html#\1#g'`
mv ${b}oldboy.html ${b}oldgirl.HTML
done
回到顶部

5、写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些

方法有很多种

#!/usr/bin/bash
##############################################################
# File Name: ip.sh
# Version: V1.0
# Author: CentOS
# Organization: http://47.94.88.182/
# Created Time : 2018-11-09 11:07:50
# Description:
##############################################################

source /etc/init.d/functions
IP=10.0.0.
for j in {1..254}
do
ping -c 1 -w 2s $IP$j &>/dev/null

if [ $? -eq 0 ];then
        action "$IP$j " 
else
        action "$IP$j" /bin/false
fi
done
回到顶部

6、请用至少两种方法实现!循环、数组方式

for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。

I am oldboy teacher welcome to oldboy training class.

方法一

#!/bin/sh
for i in I am oldboy teacher welcome to oldboy training class.
do
        if [ ${#i} -lt 6 ];then
        echo $i
        fi
done

方法二

echo "I am oldboy teacher welcome to oldboy training class."|xargs -n1|awk '{if(length<6)print}'

方法三

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

7、开发shell脚本分别实现以脚本传参以及read读入的方式比较2个整数大小。

以屏幕输出的方式提醒用户比较结果。当用脚本传参以及read读入的方式需要对变量是否为数字、并且传参个数做判断。 read读入的方式

#!/bin/bash
read -p "Please input two Number: " -a Arr_str
echo ${Arr_str[*]} | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit 
if [ ${#Arr_str[*]} -eq 2 ];then
   if [ ${Arr_str[0]} -eq ${Arr_str[1]} ];then
        echo "${Arr_str[0]} == ${Arr_str[1]}"
   elif [ ${Arr_str[0]} -gt ${Arr_str[1]} ];then
        echo "${Arr_str[0]} > ${Arr_str[1]}"
   else
        echo "${Arr_str[0]} < ${Arr_str[1]}"
   fi
else
    echo "Please input two Number" 
fi

脚本传参的方式

#!/usr/bin/bash
##############################################################
# File Name: panduan.sh
# Version: V1.0
# Author: CentOS
# Organization: http://47.94.88.182/
# Created Time : 2018-11-13 20:12:54
# Description: 
##############################################################
   
echo $1 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit
echo $2 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit
if [ $# -eq 2 ];then
   if [ $1 -eq $2 ];then
        echo "$1 == $2"
   elif [ $1 -gt $2 ];then
        echo "$1 > $2"
   else
        echo "$1 < $2"
   fi
else
    echo "Please input two Number" 
fi
回到顶部

8、面试及实战考试题:监控web站点目录下所有文件

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

find /var/html/www/ -type f -name "*.thml"|xargs md5sum >/tmp/a.log
md5sum -c /tmp/a.log
回到顶部

9、老男孩教育天津项目学生实践抓阄题目:

好消息,老男孩培训学生外出企业项目实践机会(第6次)来了(本月中旬),但是,名额有限,队员限3人(班长带队)。 因此需要挑选学生,因此需要一个抓阄的程序:

要求:
1、执行脚本后,想去的同学输入英文名字全拼,产生随机数01-99之间的数字,数字越大就去参加项目实践,前面已经抓到的数字,下次不能在出现相同数字。
2、第一个输入名字后,屏幕输出信息,并将名字和数字记录到文件里,程序不能退出继续等待别的学生输入,抓完输入exit退出。
#!/usr/bin/bash
##############################################################
# File Name: ces.sh
# Version: V1.0
# Author: CentOS
# Organization: http://47.94.88.182/
# Created Time : 2018-11-12 16:22:39
# Description:
##############################################################

declare -A arr
arr[banzhang]=100

while true
do
    [ "$Name" = "quit" ] && break

    read -p "Please input you name: " Name

    while true 
    do
      Num=`echo $(($RANDOM%100+1))`
      if [[ $Num =~ ${arr[*]} ]];then
          continue
      else
          arr[$Name]=$Num
          echo "$Name" "$Num"
          break
      fi
    done

done

for j in ${!arr[*]}
do
  echo $j ${arr[$j]} >>/tmp/zhuajiu.txt
done

sort -nr -k2 /tmp/zhuajiu.txt|head -4 |column -t
回到顶部

10、计算从1加到100之和

seq 100|awk '{i=i+$1}END{print i}'
作者:合合合衬 出处:www.cnblogs.com/wenrulaogou… 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.