我有一个文本文件done.txt,其中包含我需要执行某些操作的文件路径。 这是我done.txt的第一部分:
/data/voice/Voice27Folds/1000/1017_009.flac
/data/voice/Voice27Folds/1000/1017_010.flac
/data/voice/Voice27Folds/1000/1017_013.flac
/data/voice/Voice27Folds/1000/1017_014.flac
/data/voice/Voice27Folds/1000/1017_015.flac
/data/voice/Voice27Folds/1000/1017_020.flac
/data/voice/Voice27Folds/1000/1017_025.flac
/data/voice/Voice27Folds/1000/1017_026.flac
/data/voice/Voice27Folds/1000/1017_027.flac
/data/voice/Voice27Folds/1000/1017_030.flac
.....
当我运行此命令时:
while IFS= read -r var
do
echo "$var"
done < /data/voice/done.txt
我完全掌握了done.txt:
/data/voice/Voice27Folds/1000/1017_009.flac
/data/voice/Voice27Folds/1000/1017_010.flac
/data/voice/Voice27Folds/1000/1017_013.flac
/data/voice/Voice27Folds/1000/1017_014.flac
/data/voice/Voice27Folds/1000/1017_015.flac
/data/voice/Voice27Folds/1000/1017_020.flac
/data/voice/Voice27Folds/1000/1017_025.flac
/data/voice/Voice27Folds/1000/1017_026.flac
/data/voice/Voice27Folds/1000/1017_027.flac
/data/voice/Voice27Folds/1000/1017_030.flac
....
然而,当我尝试:
while IFS= read -r var
do
mv "$var" /data/voice #move all files to this directory
done < /data/voice/done.txt
我收到大量与目录相关的错误,如下所示:
mv: cannot stat '/data/voice/Voice27Folds/1000/1017_009.flac\r': No such file and directory
mv: cannot stat '/data/voice/Voice27Folds/1000/1017_013.flac\r': No such file and directory
...
我注意到正确的目录可能已更改,并在末尾附加了“\ _”。我该如何解决这个问题?
答案 0 :(得分:3)
$'\r'
是回车符 - 将光标发送回行首的指令。
在DOS(和Windows)上,文本文件的每一行以$'\r\n'
(CRLF)结尾,而在UNIX上,每行仅以$'\n'
结尾。因此,在UNIX上读取DOS文本文件时,每行似乎都有一个额外的$'\r'
。
因为当echo
打印该字符时,它会将光标移动到行的开头而不是创建任何类型的可见输出,因此无法使用echo "$var"
区分存在拖尾回车情况(除非你在它之后打印任何内容 - echo "[$var]"
会将]
放在行的开头而不是结尾处。如果要以直观明确的方式打印显示隐藏字符的可变内容,请改用printf 'var=%q\n' "$var"
。
while IFS= read -r var; do
var=${var%$'\r'} # remove any trailing carriage return from var
mv -- "$var" /data/voice #move all files to this directory
done < /data/voice/done.txt