1. 程式人生 > >自動化---建立指定格式檔案(小寫字母_wana.txt)

自動化---建立指定格式檔案(小寫字母_wana.txt)

指定目錄下通過隨機小寫指定多少個字母加固定字串wana批量建立10個以.txt 檔案例如:aabb_wanan.txt(指定在4個小寫字母)

#!/bin/bash
#用法:-d filename  如果 filename存在且為目錄,則為真 
#cut -c 1-4 以字元為單位進行分割,取前1-4個字元
#tr "要替換的字元"  "替換成什麼"
read -p "please enter a dir :" dir
read -p "please enter create many file:" num1
read -p "Please enter Lowercase letters(how many lowercase)  :" num2
read -p "please enter   Suffix name:" end
 if [ -d $dir ]
     then
      echo "目錄存在"
      else
      mkdir -p $dir
      fi
      cd $dir
      for(( i=1;i<="$num1";i++))
      do
       name="`echo $RANDOM|md5sum|cut -c 1-"$num2"|tr "[0-9]" "[a-z]"`_wanan.$end"
       touch $name
        echo "$name is Creating ..........."

執行結果: 在這裡插入圖片描述