Shell programming interview must be 30 questions

Source: Internet
Author: User


Source: "Learn Linux with old boys" shell programming combat


Interview Question 1: mass production of random character filenames

Code:

[email protected]:/home/dell/shell# vim creat_ten_htmlfile.sh #!/bin/bash#date:  2017-8-25#author: xianweipath=/tmp/shelltmp[ -d  "$Path"  ] | |  mkdir -p  $Path                   #如果测试结果为假, execute mkdir statement cd  $Pathfor ((i=0;i<10;i++))                                 #循环执行10次do         namepre= ' date +%s |md5sum|tr -dc  "A-Z" '    #生成随机的10个字母         namepost = ' _oldboy.html '                    #文件名的后缀         filename=${namepre}${namepost}            #字符String Connection         touch  $filename          echo  "$filename  have been created."         sleep 1done &wait


Test

[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 8drwxr-xr-x  2 root root 4096 aug 25 07:53 ./ DRWXRWXRWT&NBSP;15&NBSP;ROOT&NBSP;ROOT&NBSP;4096&NBSP;AUG&NBSP;25&NBSP;08:05&NBSP, .... /-rw-r--r--  1 root root &Nbsp;  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


Summarize:

Examine the Knowledge points:

1) method for generating random numbers

2) method of string connection


Interview Question 2: Change the string Oldboy in question 1 to Oldgirl


Method One: Use awk to generate the required commands, and then use Bash to execute

Code and testing:

[Email protected]:/tmp/shelltmp# for i in ' ls/tmp/shelltmp ';d o echo $i |awk-f "_" ' {print "MV" $ "" $ "_oldgirl.html" } ' |bash;d one[email protected]:/tmp/shelltmp# lsadcaeeafbc_oldgirl.html beadcabbbdcbdb_oldgirl.html edffdbddee_ oldgirl.html fcaadeaedafbc_oldgirl.htmladddadbc_oldgirl.html deffebcd_oldgirl.html Fafbbcdcfcfecef_oldgi rl.htmlbcadafebdabe_oldgirl.html ecbdeabdaedceb_oldgirl.html Fbfdedccbcc_oldgirl.html[email protected]:/tmp/ shelltmp#


Method 2: Replace the file name with SED, and then change the file name with MV

Code

#!/bin/bash#date:2017-8-25#author:xianweipath=/tmp/shelltmp[-D "$Path"] | | Exit 0 #如果测试结果为假, execute mkdir statement CD $Pathfor oldfile in ' ls $Path/|grep oldboy ' do newfile= ' echo $oldfile | Sed s/oldboy/oldgirl/g ' #生成新的文件名 mv $oldfile $newfiledone &wait



Interview 3: Create users and Passwords in bulk


Code

[email protected]:/home/dell/shell# vim 3_create_user.sh  #!/bin/bash#date:  2017-8-25#author: xianwei#create ten users in bulkfor ((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  Passworddone


Test

[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 je28zqtioldboy1 Lca5ax3uoldboy2 qf36poh2oldboy3 5bmoklfpoldboy4 18slv8fboldboy5 8eivwck3oldboy6 ZuWQcqjToldboy7 lSeahDHMoldboy8 XvqBiFPAoldboy9 Vnc8flzcoldboy10 Zdbruc2s




To be continued .....


























This article is from the "Hello World" blog, so be sure to keep this source http://237085.blog.51cto.com/227085/1959480

Shell programming interview must be 30 questions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.