我在一个文件夹中有一堆文件,其名称以以下开头MyTest
:
MyTestHttpAdaptor.class
MyTestJobCreation.class
我想使用删除MyTest
前缀的名称创建这些文件的副本,并添加Test
后缀:
MyHttpAdaptorTest.class
MyJobCreationTest.class
如何才能做到这一点?
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}
是参数扩展的示例,删除前缀和后缀。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句