命令以相同的char终止

时间:2016-02-17 14:58:55

标签: linux bash shell

如何修复以下行:

for line in $(cat /root/files_to_search); do find /opt/ -iname $line -exec rm {} >> files_founded.txt ; done

问题是for-exec的命令以分号结尾,for命令有此语句

for i in something; do command ; done (do terminate with ;)
带有find

-exec声明

find /path/ -name somestring -exec command \; (-exec also finish with ;)

1 个答案:

答案 0 :(得分:2)

您甚至不需要使用-exec echo {},因为这是find中的默认操作。您可以使用此for循环:

while IFS= read -r line; do
    find /opt/ -iname "$line"
done < /root/files_to_search >> files_founded.txt

您无需使用for line in $(cat ...),因为您可以使用< file来读取输入

See BASH FAQ on reading a file line by line

如果您必须在-exec中使用find,请使用:

while IFS= read -r line; do
    find /opt/ -iname "$line" -exec rm {} \;
done < /root/files_to_search >> files_founded.txt