来源说明:《跟老男孩学Linux运维》Shell编程实战

说明:对于每个脚本,用shell编程的同时,我再用python实现,达到同时熟悉两种脚本语言的目的。由于初学python,有问题还请大家斧正。


面试题1:批量生产随机字符文件名

用shell实现

代码:

root@vmUbu:/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


测试

root@vmUbu:/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.
root@vmUbu:/home/dell/shell# 
root@vmUbu:/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)字符串连接的方法


使用Python实现

代码

#encoding:utf-8

import random
import os

def get10char():                                        #create 10 random charactors
  seed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  sa = []
  for i in xrange(10):
    sa.append(random.choice(seed))
  salt = ''.join(sa)

  return salt

def createfile(name):
  path="/tmp/shelltmp/"                                 #check whether the dir is exists,if not create it
  if os.path.exists(path) != True:
    os.mkdir(path)

  pathAndName=path+name
  with open(pathAndName,"wb") as f:                     #create file
    pass
  if os.path.exists(pathAndName):
    print "%s have been created" %name                  #print the result
  else:
    print "create file failed,Please check it."


def main():
  for i in xrange(10):                                  #loop 10 times to create 10 file
    filename=get10char()+"_oldboy.html"
    createfile(filename)


if __name__ == "__main__":
        main()



面试题2:将面试题1中的字符串oldboy全部改为oldgirl


方法一:使用awk生成所需命令,再用bash执行

代码和测试:

root@vmUbu:/tmp/shelltmp# for i in `ls /tmp/shelltmp`;do echo $i|awk -F "_" '{print "mv " $0 " " $1"_oldgirl.html" }' |bash ;done

root@vmUbu:/tmp/shelltmp# ls
adcaeeafbc_oldgirl.html    beadcabbbdcbdb_oldgirl.html  edffdbddee_oldgi