天天看點

幾道shell面試題及答案

企業面試題1:

已知下面的字元串是通過RANDOM随機數變量md5sum|cut-c 1-8截取後的結果,請破解這些字元串對應的md5sum前的RANDOM對應數字?

21029299

00205d1c

a3da1677

1f6d12dd

890684b

    解題思路:通過每次傳遞一個參數的方式,來實作依次破解

              $RANDOM的範圍為0-32767

        #!/bin/bash
        #Author: liuwei
        #Site: www.51liuzw.com
        for n in {0..32767}
        do
            MD5=`echo $n | md5sum | cut -c 1-8`
            if [ "$MD5" == "$1" ];then
                echo "$n is."
                exit
            else
                echo "$n no."
            fi
        done      

     注:也可以通過定義數組的方法,來一次全部對比

        #!/bin/bash    
        #Author: liuwei
        #Site: www.51liuzw.com
        array=(
                00205d1c
                21029299
                a3da1677
                1f6d12dd
                890684b
        )
        for n in {0..32767}
        do
                MD5=`echo $n | md5sum | cut -c 1-8`
                for i in ${array[@]};do
                        if [ "$MD5" == "$i" ];then
                                echo "$n and $i" >> c.log
                                break
                        else
                                echo "$n no."
                        fi
                done
        done      

          #cat c.log 

         1346 and 00205d1c

         7041 and 1f6d12dd

         25345 and a3da1677

         25667 and 21029299

企業面試題2:批量檢查多個網站位址是否正常 

要求:shell數組方法實作,檢測政策盡量模拟使用者通路思路

http://www.baidu.com

http://www.taobao.com

http://www.51liuzw.com

http://192.168.50.199

#!/bin/bash
#Author: liuwei
#Site: www.51liuzw.com
array=(
http://www.baidu.com
http://www.taobao.com
http://www.51liuzw.com
http://192.168.50.199
)
for n in ${array[*]};do
        URL=`curl -I -m 2 $n 2> /dev/null | egrep "200|302" | wc -l`
        if [ "$URL" -eq 1 ];then
                echo "$n is OK"
        else
                echo "$n is not OK"
        fi
done      

結果:

    #sh test.sh

    http://www.baidu.com is OK

    http://www.taobao.com is OK

    http://www.51liuzw.com is not OK

    http://192.168.50.199 is not OK

企業面試題3::用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.按單詞出現頻率降序排序!

        解決思路:把空格換為換行符,并排序統計   

      #sed 's# #\n#g' c.txt | sort | uniq -c      

    2.按字母出現頻率降序排序!

        解決思路:使用grep -o "\w",把單詞拆開并去除種各符号

      #cat c.txt | grep -o "\w" | sort | uniq -c      

繼續閱讀