cayne
cayne

Reputation: 3

Problems with BASH variable multiplication inside if statment

I am having an issue getting BASH to update a variable in a piece of scripting that I am working on. Below is an excerpt from my script that I have having troubles with

read -p "Did you plan on running the server from a Ramdisk?(y/n)" RAMDISK
        if [[ "$RAMDISK" == [yY] ]]; then
                read -p "What is the full path to where you would like to create the ramdisk?(/var/RAMDISK)" RAMDIR
                read -p "How big did you want the ramdisk to be?(2048)" RAMSIZE
                if [[ "$RAMSIZE" -lt 1024 ]]; then
                        $NEWRAMSIZE = $((RAMSIZE*1024)) #FIXME
                        echo $NEWRAMSIZE
                fi
                RAMDISK="Enabled"
        fi

        if [[ "$RAMDISK" == [nN] ]]; then
                RAMDISK="Disabled"
        fi
        echo $NEWRAMSIZE

My problem is that no matter what I am trying, the RAMSIZE variable never gets multiplied by 1024. I am trying to make it so no matter what number you enter, it will always get changed to a size in mb instead of gb, assuming anything less than 1024 is a gb size. Earlier in the script RAMSIZE defaults to 2048 and it never seems to be changed during the if statement. Please help?

Upvotes: 0

Views: 534

Answers (1)

Gordon Davisson
Gordon Davisson

Reputation: 125838

I see three problems: you shouldn't have spaces around the = in an assignment, you shouldn't use $ on the variable you're assigning to ($variable is how you get the value of a variable, not how you set it), and you're using RAMSIZE in some places and NEWRAMSIZE in others. The critical lines are:

if [[ "$RAMSIZE" -lt 1024 ]]; then
    RAMSIZE=$((RAMSIZE*1024)) # Note: no spaces, no extra $, same variable
    echo $NEWRAMSIZE
fi

...

echo "$RAMSIZE" # Same variable

BTW, you can use shellcheck.net to spot basic problems like this.

Upvotes: 2

Related Questions