我的文件夹中有一堆文件,文件名以MyTest
开头:
MyTestHttpAdaptor.class
MyTestJobCreation.class
我想使用以下名称创建这些文件的副本:删除MyTest
前缀,并添加一个Test
后缀:
MyHttpAdaptorTest.class
MyJobCreationTest.class
这怎么办?
答案 0 :(得分:1)
for file in MyTest*.*; do # iterate over files that start with MyTest and have a .
ext=${file##*.} # remove everything before the last . to get the extension
basename=${file%.*} # remove everything *after* the last . to get the "basename"
new_basename=${basename#MyTest} # remove the prefix to get the *new* basename
new_file="${new_basename}Test.$ext" # combine that prefix with the "Test" suffix & ext.
[[ -e $new_file ]] || cp -- "$file" "$new_file" # copy if result does not already exist
done
${file##*.}
,${file%.*}
和${basename#MyTest}
是parameter expansion的示例,其中删除了前缀和后缀。