Bash - 文件存在提供不正确的结果

标签 bash file-exists

我有一个包含文件列表的文件,我想知道这些文件是否存在。我使用了这个命令:

while read line; do 
    filename="$(echo $line | cut -d';' -f4)"; 
    if [ ! -e "/some/path/$filename" ]; 
    then echo "/some/path/$filename"; 
    fi ; 
done < "../my_list_of_file"

此命令返回给我列出的每个不存在的文件,例如:

/some/path/my_listed_file.jpg

但是当我执行ls/some/path/my_listed_file.jpg时,我可以看到该文件存在。我的命令有什么问题?

最佳答案

感谢 Barmar 和 chepner,问题是文件末尾的 CRLF。 这是工作命令:

while IFS=$';\r' read -r _ _ _ filename _; do 
    if [ ! -e "/some/path/$filename" ]; 
    then echo "/some/path/$filename"; 
    fi ; 
done < "../my_list_of_file"

关于Bash - 文件存在提供不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40027428/

相关文章:

linux - 如何在 Linux 中使用 grep 找出一行中的单词?

php - 使用子流程传递时不接受两个输入

matlab - 在 MATLAB 中存在(x, 'file')需要永远

php - 使用 php 在网络上使用 ip 或计算机名读取文件

php - file_exists() 在 PHP 中太慢了。任何人都可以提出更快的替代方案吗?

Shell脚本读取文件路径直到文件存在

bash - 连接行,对记录数取模

linux - 如何通过网络写入文件

bash - 是否有任何简单的方法来检查 Bash 中是/否选项的所有可能性?