我有一个脚本,如下。
要处理的文件存储在一个文件中images.txt
,该文件逐行读取。第一个echo
命令正确显示文件名,但随后的ImageMagick命令无法处理图像,表明未找到该文件。为什么?
#!/bin/bash
filename="images.txt"
while read -r line
do
echo "line is $line"
# width
width="$( identify -format "%w" "$line" )"
# height
height="$( identify -format "%h" "$line" )"
echo "$width X $height "
exit 1
if [ $width -lt 250 -a $height -lt 250 -a $width -lt $height ]
then
echo "1"
convert $line -resize 250 $line
elif [ $width -lt 250 -a $height -lt 250 -a $width -gt $height ]
then
echo "2"
convert $line -resize x250 $line
elif [ $width -lt 250 ]
then
echo "3"
convert $line -resize 250 $line
elif [ $height -lt 250 ]
then
echo "4"
convert $line -resize x250 $line
else
echo "All is Well"
fi
done < "$filename"
输出:
line is v/347/l_ib-dfran035__62594_zoom.jpg
': No such file or directory @ error/blob.c/OpenBlob/2589._zoom.jpg
从错误行(': No such file...
而不是'filename': No such file...
)来判断,问题可能出在您的images.txt
文件中,该文件的行以CR-LF终止(即,images.txt
来自Windows世界)。
因此,您的line
变量(文件名)以CR
不正确的(回车符)(没有此类文件...)结尾。而且,当它显示在屏幕上时,由于嵌入了控制字符CR
,所以打印从行的开头继续,并覆盖文件名。
更改的格式images.txt
,使其行以LF终止(dos2unix
例如,使用该实用程序),或以bash过滤掉结尾的CR。
[更新]如果您选择在bash中过滤掉CR,则理想情况下可以执行以下任一操作:
read -r -d $'\r' line
read -r line
:line=${line%$'\r'}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句