PDA

View Full Version : bash script: variable assignment inside an if



jordilin
January 16th, 2007, 04:54 PM
I have the following:

if [ -f $NAME=$dir"/"$NEWNAME\_$j".jpg" ]
then
echo "file already exists"
exit 0
else
echo $NAME
mv $i $NAME
fi
let "j+=1"

but, I think that the NAME variable is not correct inside the if. Is there a way to do an assignment inside an if? Thanks

hal10000
January 16th, 2007, 06:13 PM
You may have to quote the $NAME


if [ -f "$NAME"=$dir"/"$NEWNAME\_$j".jpg" ]


EDIT: I'm not too sure, but maybe you have to quote the $dir, $NEWNAME and $j variables too:



if [ -f "$NAME"="$dir""/""$NEWNAME"\_"$j"".jpg" ]

or a shorter version:


if [ -f "$NAME"="$dir\/$NEWNAME\_$j.jpg" ]

jordilin
January 16th, 2007, 07:15 PM
thanks