1:使用for循环在/tmp/file下批量创建10个文件每个文件包含十个随机字符加固定字符的.txt文件,执行脚本后查看/tmp/file获得如下结果
[root@test 30]# ls /tmp/file/ bhfhddagkd_test.txt ecaajfbekf_test.txt hbgbjciekh_test.txt cieehgffkd_test.txt faghebiike_test.txt ijaddfacka_test.txt dcehifffkf_test.txt fdaefebekc_test.txt dgdacjfeka_test.txt gfagbdbbka_test.txt
解答:
file(){ filename=`uuidgen|tr '0-9-' 'a-z'|cut -c -10` } dir=/tmp/file [ -d "$dir" ] || mkdir -p /tmp/file for((i=1;i<=10;i++));do cd $dir file touch ${filename}_test.txt done
2.在上题的基础上,将*_test.txt改为*_web.html格式,完成效果如下
[root@test 30]# ls /tmp/file/ bhfhddagkd_web.html ecaajfbekf_web.html hbgbjciekh_web.html cieehgffkd_web.html faghebiike_web.html ijaddfacka_web.html dcehifffkf_web.html fdaefebekc_web.html dgdacjfeka_web.html gfagbdbbka_web.html
解答:
dir=/tmp/file if [ -d "$dir" ];then cd $dir else echo "error" fi for i in `ls *.txt` ; do file_new=`echo $i|cut -c -10` mv $i ${file_new}_web.html done