How do I set a variable inside a bash for loop?

Posted by Isaac Moore on Stack Overflow See other posts from Stack Overflow or by Isaac Moore
Published on 2010-12-31T15:52:29Z Indexed on 2010/12/31 15:53 UTC
Read the original article Hit count: 247

Filed under:
|
|
|

I need to set a variable inside of a bash for loop, which for some reason, is not working for me. Here is an excerpt of my script:

function unlockBoxAll
{
appdir=$(grep -i "CutTheRope.app" /tmp/App_list.tmp)
for lvl in {0..24}
key="UNLOCKED_$box_$lvl"
plutil -key "$key" -value "1" "$appdir/../Library/Preferences/com.chillingo.cuttherope.plist" 2>&1> /dev/null
successCheck=$(plutil -key "$key" "$appdir/../Library/Preferences/com.chillingo.cuttherope.plist")
if [ "$successCheck" -eq "1" ]; then
 echo "Success! "
    else
 echo "Failed: Key is $successCheck "
fi
done
}

As you can see, I try to write to a variable inside the loop with:

key="UNLOCKED_$box_$lvl"

But when I do that, I get this:

/usr/bin/cutTheRope.sh: line 23: syntax error near unexpected token `key="UNLOCKED_$box_$lvl"'
/usr/bin/cutTheRope.sh: line 23: `key="UNLOCKED_$box_$lvl"'

What am I not doing right? Is there another way to do this?

Please help, thanks.

© Stack Overflow or respective owner

Related posts about error

Related posts about bash