I am very new to shell scripts and I have a basic image resizing script:
#!/bin/bash
isResizeAnotherImage=Y;
for isResizeAnotherImage in Y y N n
do
echo "option entered before if: $isResizeAnotherImage";
if [ $isResizeAnotherImage='Y' ] || [ $isResizeAnotherImage='y' ]; then
echo "Enter the file name : ";
read imageFileName;
read -p "Enter desired size: " imageDesiredSize;
mogrify -resize $imageDesiredSize $imageFileName 2>/dev/null;
echo "Resize another image ? Y/N : ";
read isResizeAnotherImage;
echo "option entered in if: $isResizeAnotherImage";
else
echo "option entered : $isResizeAnotherImage";
exit 1;
fi;
done
Now, It resizes first image successfully, asks ‘Resize another image?’. Here, I enter ‘N’. But in the next loop, it shows that the value of flag has not been changed to ‘N’. So it enters the if condition.
Why is this happening and how to prevent it ?
Your indenting is a bit off which makes it hard to read your code.
Ignoring that, what is your for-loop for? Is that supposed to be a while-loop? As I see it this is going to make your variable isResizeAnotherImage take the values YyNn and ignore what you have entered at the prompt. Change your for-loop to a while-loop and I suspect it will work.