代码之家  ›  专栏  ›  技术社区  ›  Brett

Shell脚本从txt文件查找和复制

  •  0
  • Brett  · 技术社区  · 7 年前

    sh script.sh $line 每个循环迭代的变量字符串正确。运行相同的 find $list 变量确实成功完成了任务。

    script.sh
    #!/bin/bash
    file="filenames.txt"
    while IFS= read line
    do
    find /Volumes/share/all-files-location -name "$line" -print -exec cp {} /Volumes/share/found-files-location \;
    done < "$file"
    

    A1037_MG_1262.jpg
    A1037_MG_12621.jpg
    A1037_MG_1263.jpg
    [11,000 more entries]
    

    1 回复  |  直到 7 年前
        1
  •  0
  •   Charles Srstka    7 年前

    由于输入文件位于CRLF中,Unix通常使用LF作为其行结束符,因此bash脚本根据LF字符的出现情况对行进行排序,因此每个文件名都具有 \r 这个 find 工具没有找到任何文件,结果是你的工具似乎失败了,没有给出错误消息。