shell编程面试必会30题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell编程面试必会30题相关的知识,希望对你有一定的参考价值。
来源:《跟老男孩学Linux运维》Shell编程实战
面试题1:批量生产随机字符文件名
代码:
[email protected]:/home/dell/shell# vim creat_ten_htmlfile.sh #!/bin/bash #Date: 2017-8-25 #Author: XianWei Path=/tmp/shelltmp [ -d "$Path" ] || mkdir -p $Path #如果测试结果为假,就执行mkdir语句 cd $Path for((i=0;i<10;i++)) #循环执行10次 do namepre=`date +%s|md5sum|tr -dc "a-z"` #生成随机的10个字母 namepost=‘_oldboy.html‘ #文件名的后缀 filename=${namepre}${namepost} #字符串连接 touch $filename echo "$filename have been created." sleep 1 done & wait
测试
[email protected]:/home/dell/shell# ./creat_ten_htmlfile.sh adcaeeafbc_oldboy.html have been created. ecbdeabdaedceb_oldboy.html have been created. edffdbddee_oldboy.html have been created. beadcabbbdcbdb_oldboy.html have been created. fcaadeaedafbc_oldboy.html have been created. adddadbc_oldboy.html have been created. bcadafebdabe_oldboy.html have been created. deffebcd_oldboy.html have been created. fafbbcdcfcfecef_oldboy.html have been created. fbfdedccbcc_oldboy.html have been created. [email protected]:/home/dell/shell# [email protected]:/home/dell/shell# ll /tmp/shelltmp/ total 8 drwxr-xr-x 2 root root 4096 Aug 25 07:53 ./ drwxrwxrwt 15 root root 4096 Aug 25 08:05 ../ -rw-r--r-- 1 root root 0 Aug 25 07:53 adcaeeafbc_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 adddadbc_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 bcadafebdabe_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 beadcabbbdcbdb_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 deffebcd_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 ecbdeabdaedceb_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 edffdbddee_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 fafbbcdcfcfecef_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 fbfdedccbcc_oldboy.html -rw-r--r-- 1 root root 0 Aug 25 07:53 fcaadeaedafbc_oldboy.html
总结:
考察知识点:
1)生成随机数的方法
2)字符串连接的方法
面试题2:将面试题1中的字符串oldboy全部改为oldgirl
方法一:使用awk生成所需命令,再用bash执行
代码和测试:
[email protected]:/tmp/shelltmp# for i in `ls /tmp/shelltmp`;do echo $i|awk -F "_" ‘{print "mv " $0 " " $1"_oldgirl.html" }‘ |bash ;done [email protected]:/tmp/shelltmp# ls adcaeeafbc_oldgirl.html beadcabbbdcbdb_oldgirl.html edffdbddee_oldgirl.html fcaadeaedafbc_oldgirl.html adddadbc_oldgirl.html deffebcd_oldgirl.html fafbbcdcfcfecef_oldgirl.html bcadafebdabe_oldgirl.html ecbdeabdaedceb_oldgirl.html fbfdedccbcc_oldgirl.html [email protected]:/tmp/shelltmp#
方法2:使用sed替换文件名,再用mv修改文件名
代码
#!/bin/bash #Date: 2017-8-25 #Author: XianWei Path=/tmp/shelltmp [ -d "$Path" ] || exit 0 #如果测试结果为假,就执行mkdir语句 cd $Path for oldfile in `ls $Path/ |grep oldboy` do newfile=`echo $oldfile | sed s/oldboy/oldgirl/g` #生成新的文件名 mv $oldfile $newfile done & wait
面试题3:批量创建用户和密码
代码
[email protected]:/home/dell/shell# vim 3_create_user.sh #!/bin/bash #Date: 2017-8-25 #Author: XianWei #create ten users in bulk for((i=0;i<=10;i++)) do username="oldboy${i}" #cannot use symbol ‘‘ password=`tr -dc "a-zA-Z0-9" </dev/urandom |head -c 8` #create 8 random charactors #echo $username useradd $username echo $username:$password |chpasswd #change passwd.if os=centos,use passwd --stdin #echo ${username}" "${password} echo ${username}" "${password} >> passwd.txt #record the username and it‘s password done
测试
[email protected]:/home/dell/shell# ./3_create_user.sh [email protected]:/home/dell/shell# cat /etc/passwd ...... hello:x:1001:1001::/home/hello: oldboy0:x:1002:1002::/home/oldboy0: oldboy1:x:1003:1003::/home/oldboy1: oldboy2:x:1004:1004::/home/oldboy2: oldboy3:x:1005:1005::/home/oldboy3: oldboy4:x:1006:1006::/home/oldboy4: oldboy5:x:1007:1007::/home/oldboy5: oldboy6:x:1008:1008::/home/oldboy6: oldboy7:x:1009:1009::/home/oldboy7: oldboy8:x:1010:1010::/home/oldboy8: oldboy9:x:1011:1011::/home/oldboy9: oldboy10:x:1012:1012::/home/oldboy10: [email protected]:/home/dell/shell# #查看密码文件 [email protected]:/home/dell/shell# cat passwd.txt oldboy0 Je28ZqTi oldboy1 LcA5AX3u oldboy2 QF36POh2 oldboy3 5BMoklFp oldboy4 18slv8fB oldboy5 8eIVWck3 oldboy6 ZuWQcqjT oldboy7 lSeahDHM oldboy8 XvqBiFPA oldboy9 VNc8fLZC oldboy10 zdbruc2S
未完待续.......
本文出自 “hello world” 博客,请务必保留此出处http://237085.blog.51cto.com/227085/1959480
以上是关于shell编程面试必会30题的主要内容,如果未能解决你的问题,请参考以下文章
合格linux运维人员必会的30道shell编程面试题及讲解-08
合格linux运维人员必会的30道shell编程面试题及讲解-09
合格linux运维人员必会的30道shell编程面试题及讲解