简体   繁体   English

如何在for循环中使用echo或printf仅打印一次出现

[英]How to print only one occurence with echo or printf in a for loop

I have a folder TEST with some files (with the occurence abc ) I want to delete.我有一个文件夹TEST ,其中包含我想删除的一些文件(出现abc )。 I use :我用 :

for filename in TEST/*; do  
   if [ -f "$filename" ]; then 
       if [[ "$filename" == *abc* ]]; then  
             rm $filename  && echo "The files $filename are well deleted" ; 
             else printf  "The files were already deleted" ; 
       fi; 
   fi; 
done

What I want, is to print the "The files were already deleted" only once .我想要的是只打印一次"The files were already deleted" For example, after removing the files with the occurence abc , if it remains 4 files inside the folder TEST , that will print the sentence "The files were already deleted" 4 times, and not one like I would like to.例如,删除出现abc的文件后,如果在TEST文件夹中保留 4 个文件,则会打印 4 次"The files were already deleted"这句话,而不是像我想要的那样。

Any idea?任何的想法?

I don't understand why you are using a loop for this.我不明白你为什么为此使用循环。

rm TEST/*abc*

should do it, though it won't report each file.应该这样做,尽管它不会报告每个文件。 It'll maybe complain about directories that match.它可能会抱怨匹配的目录。

If you need the report -如果你需要这份报告——

found=0
while IFS= read -rd '' f
do rm $f && echo "deleted '$f'"
   found=1
done < <( find TEST/ -name '*abc*' -type f -print0 )
(( found )) || echo "The files were already deleted"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM