我有一个图像调整器(底部)。现在我想用它来批量调整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/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
你的for
循环没有扩展到预期的,因为find
没有被调用。
相反,find
命令的每个单词(包括参数)都用作for
循环的参数。
一种解决方案是find
在子 shell 中使用,如
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
/Users/dev/scripts/./imre.sh "$image" 200 133
done
您可以使用以下命令轻松检查它echo
:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句