老男孩出的Shell编程企业面试题3:
将“/oldboy/”目录下的文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。
[root@oldboy oldboy]# ls coaolvajcq_oldboy.html qnvuxvicni_oldboy.html vioesjmcbu_oldboy.html gmkhrancxh_oldboy.html tmdjormaxr_oldboy.html wzewnojiwe_oldboy.html jdxexendbe_oldboy.html ugaywanjlm_oldboy.html xzzruhdzda_oldboy.html qcawgsrtkp_oldboy.html vfrphtqjpc_oldboy.html
我的Shell脚本如下:
#!/bin/bash dir=/oldboy/ for file in $dir* do if [ -f $file ] then f1=${file##*/} f2='' if [ $f1 != ${f1/oldboy/} -o $f1 != ${f1/html/} ] then f2=${f1/oldboy/oldgirl} f2=${f2/html/HTML} cd $dir mv $f1 $f2 fi #echo $f1,$f2 fi done