Several shell interview questions and answers
Enterprise interview question 1:
It is known that the following strings are intercepted by the RANDOM random number variable md5sum | cut-c 1-8. Would you please crack the RANDOM corresponding numbers before the md5sum of these strings?
21029299
00205d1c
A3da1677
1f6d12dd
890684b
The idea of solving the problem: by passing one parameter at a time, it can be cracked in turn.
The range of $RANDOM is 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
Note: you can also compare them all at once by defining an array.
#! / 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
Enterprise interview question 2: check whether multiple website addresses are normal in batch
Requirements: shell array method implementation, detection strategy as far as possible to simulate user access ideas
Http://www.baidu.com
Http://www.taobao.com
Http://www.51liuzw.com
Http://192.168.50.199
#! / bin/bash#Author: liuwei#Site: www.51liuzw.comarray= (http://www.baidu.comhttp://www.taobao.comhttp://www.51liuzw.comhttp://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" fidone
Results:
# 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
Enterprise interview question 3: deal with the following with shell
1. Sort by the frequency of words in descending order!
2. Sort by alphabetical frequency in descending order!
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. Sort by word frequency in descending order!
Solution: replace spaces with newline characters and sort statistics
# sed's # #\ nroomg' c.txt | sort | uniq-c
two。 Sort by alphabetical frequency in descending order!
Solution: use grep-o "\ w" to take the words apart and remove the symbols
# cat c.txt | grep-o "\ w" | sort | uniq-c