我想重命名名称与某些后缀相匹配的目录中的文件,即810
或814
。我希望新名称包含根据后缀选择的字符串(对于814
,EB_ENROLL_REQ
;对于810
,EB_BCHG_REQ
)。
输入文件名的示例(全部在$source_dir
中)是:
CCRD_LLX_814_20160218043477.EDI810
CCRD_LLX_814_20160218043407.EDI814
helloworld
CCRD_LLX_814_20160218043487.EDI814
test123
files.txt
CCRD_LLX_814_20160218043467.EDI810
我想阅读目录中的所有文件,只重命名以814
或810
结尾的文件,忽略其余文件。
我试过了:
export search_dir=/home/test2
declare -a myArray
myArray[814]=EB_ENROLL_REQ
myArray[810]=EB_BCHG_REQ
for entry in "$search_dir"/*
do
pattern=${entry: -3}
#if ??
mv "$entry" "$entry.XHS.JOBRUNID.${myArray[$pattern]}.$entry.XHE"
done
但没有得到我需要的东西。
814
文件的输出文件名应为:
CCRD_LLX_814_20160218043487.EDI814.XHS.JOBRUNID.EB_ENROLL_REQ.CCRD_LLX_814_20160218043487.EDI814.XHE
答案 0 :(得分:3)
试试这个:
declare -A myArray # -A, not -a --- index by strings
myArray["814"]=EB_ENROLL_REQ # string suffixes, not numeric
myArray["810"]=EB_BCHG_REQ
cd "$search_dir" # Otherwise you have to strip $search_dir out of $entry
for entry in *81[04] # Only work on the files that end with 810 or 814
do
pattern=${entry: -3} # the string "810" or "814"
mv "$entry" "$entry.XHS.JOBRUNID.${myArray[$pattern]}.$entry.XHE"
done