我必须从文件中读取A.TXT,如果文件名有空间在它的文件名的list,所以能不能remove.File A.TXT在下面给出
20150726140135_Content 1.mp4
20150726162955_Content 1.mp4
20150726163056_Content 3.mp4
20150726164057_Content 5.mp4
20150726170315_Tab main 2.mp4
for f in $(cat A.txt) ; do
rm "/tmp/$f";
done
尽管试试这个文件闯入其中两个导致错误
rm: cannot remove `/tmp/20150726170315_Tab': No such file or directory
rm: cannot remove `/tmp/2': No such file or directory
分析解答
for i in $(cat)
在bash常见的反模式。你不应该使用for循环读取线。这并不工作,因为:
首先
$(cat A.txt)
扩展到20150726140135_Content 1.mp4 20150726162955_Content 1.mp4 20150726163056_Content 3.mp4 20150726164057_Content 5.mp4 20150726170315_Tab main 2.mp4
(不含引号)。然后shell执行命令
for i in 20150726140135_Content 1.mp4 20150726162955_Content 1.mp4 20150726163056_Content 3.mp4 20150726164057_Content 5.mp4 20150726170315_Tab main 2.mp4
。新行和空格都被解释为参数分隔符。
要在bash线读取文件中的行使用while IFS= read -r
循环:
while IFS= read -r f; do
rm "/tmp/$f";
done < A.txt