Bash批处理图像大小调整器
我有一个图像大小调整器(底部)。现在我想用它批量调整PNG的大小。问题是对每个文件只运行一次。此代码段在已创建的图像上创建新图像:Bash批处理图像大小调整器,bash,imagemagick,Bash,Imagemagick,我有一个图像大小调整器(底部)。现在我想用它批量调整PNG的大小。问题是对每个文件只运行一次。此代码段在已创建的图像上创建新图像: for image in find . -type f -name "*.png"; do /Users/dev/scripts/./imre.sh "$image" 200 133 done 调整器: #!/bin/bash ###### LOCAL VARIABLES convert=/usr/bin/convert identify=/usr/bin
for image in find . -type f -name "*.png"; do
/Users/dev/scripts/./imre.sh "$image" 200 133
done
调整器:
#!/bin/bash
###### LOCAL VARIABLES
convert=/usr/bin/convert
identify=/usr/bin/identify
image=$1
heigth=$2
width=$3
usage_message="Usage: imre image heigth width"
usage_example="Example: imre anyImage.png 50 50"
space=""
###### ACTIONS
clear
echo $space
echo "*** START image resizer script ***"
echo $space
if [ -z "$image" ] || [ -z "$heigth" ] || [ -z "$width" ] ;
then
echo $usage_message
echo $space
echo $usage_example
echo $space
echo "*** END image resizer script ***"
echo $space ; exit 1
fi
echo "To modify $image to $heigth x $width (HxW)"
echo "Given image data: "
identify $image
convert $image -resize "${heigth}"x"${width}" $image
echo "$1"
echo $space
echo "*** END image resizer script ***"
echo $space
exit 0
由于未调用
find
,因此您的for
循环未扩展到预期值
相反,find
命令的每个单词(包括参数)都用作for
循环的参数
解决方案是在子shell中使用find
,如中所示
for image in `find . -type f -name "*.png"`; do
/Users/dev/scripts/./imre.sh "$image" 200 133
done
或
您可以使用回音轻松地检查它:
for image in `find . -type f -name "*.png"`; do
echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done
例如,检查的结果:
for image in find . -type f -name "*.png"; do
echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done
收益率:
/Users/dev/scripts/./imre.sh "find" 200 133
/Users/dev/scripts/./imre.sh "." 200 133
/Users/dev/scripts/./imre.sh "-type" 200 133
/Users/dev/scripts/./imre.sh "f" 200 133
/Users/dev/scripts/./imre.sh "-name" 200 133
/Users/dev/scripts/./imre.sh "*.png" 200 133
由于未调用find
,因此您的for
循环未扩展到预期值
相反,find
命令的每个单词(包括参数)都用作for
循环的参数
解决方案是在子shell中使用find
,如中所示
for image in `find . -type f -name "*.png"`; do
/Users/dev/scripts/./imre.sh "$image" 200 133
done
或
您可以使用回音轻松地检查它:
for image in `find . -type f -name "*.png"`; do
echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done
例如,检查的结果:
for image in find . -type f -name "*.png"; do
echo "/Users/dev/scripts/./imre.sh \"$image\" 200 133"
done
收益率:
/Users/dev/scripts/./imre.sh "find" 200 133
/Users/dev/scripts/./imre.sh "." 200 133
/Users/dev/scripts/./imre.sh "-type" 200 133
/Users/dev/scripts/./imre.sh "f" 200 133
/Users/dev/scripts/./imre.sh "-name" 200 133
/Users/dev/scripts/./imre.sh "*.png" 200 133
只是为了简化,让其他人阅读。这是完整的配料器工作。使用imre大小调整器:
#!/bin/bash
###### LOCAL VARIABLES
heigth=$1
width=$2
usage_message="Usage: batch_imre heigth width"
usage_example="Example: batch_imre 50 50"
space=""
clear
echo $space
echo "*** START batch image resizer script ***"
echo $space
if [ -z "$heigth" ] || [ -z "$width" ] ;
then
echo $usage_message
echo $space
echo $usage_example
echo $space
echo "*** END batch image resizer script ***"
echo $space ; exit 1
fi
for image in `find . -type f -name "*.png"`; do
/Users/dev/scripts/./imre.sh "$image" $heigth $width
done
echo $space
echo "*** END batch image resizer script ***"
echo $space
exit 0
只是为了简化,让其他人阅读。这是完整的配料器工作。使用imre大小调整器:
#!/bin/bash
###### LOCAL VARIABLES
heigth=$1
width=$2
usage_message="Usage: batch_imre heigth width"
usage_example="Example: batch_imre 50 50"
space=""
clear
echo $space
echo "*** START batch image resizer script ***"
echo $space
if [ -z "$heigth" ] || [ -z "$width" ] ;
then
echo $usage_message
echo $space
echo $usage_example
echo $space
echo "*** END batch image resizer script ***"
echo $space ; exit 1
fi
for image in `find . -type f -name "*.png"`; do
/Users/dev/scripts/./imre.sh "$image" $heigth $width
done
echo $space
echo "*** END batch image resizer script ***"
echo $space
exit 0
转换“$image”-调整大小…
-缺少$
。如果你总是引用变量也是最好的。这里有一些情况下,它是不需要的,但总的来说,它是更好的…感谢评论。对不起,我正在编辑它。imre确实在运行。但第一个片段是循环的。只能执行一次。我使用@setempler first解决方案。工作起来很有魅力。感谢所有。转换“$image”-调整大小…
-缺少$
。如果你总是引用变量也是最好的。这里有一些情况下,它是不需要的,但总的来说,它是更好的…感谢评论。对不起,我正在编辑它。imre确实在运行。但第一个片段是循环的。只能执行一次。我使用@setempler first解决方案。工作起来很有魅力。谢谢大家。