Chris Grimmett
Chris Grimmett

Reputation: 297

Bash conditional expression does not work as expected inside function

I'm a noob learning as I go, and I'm stumped. Line 7 of the following snippet of code is where I'm having problems. Basically it seems like the if statement inside the function, "FUNCTION_SERVER_START" is not working. When I say, "is not working," I mean it's as if 1 = 1 should be true, but somehow 1 = 1 is false, but even when the expression is determined to be false, the script is skipping "else". It seems like the if statement, since it's inside a function, is somehow misbehaving. Hopefully I'm just doing something stupid and you can point that out.

For a bit of background on the script, I am writing it to help manage minecraft servers. I've tried to simplify what is shown here as much as possible so you can see what is going on without being slowed down by the minecraft specific stuff. I can post the full script if need be.

I can't get the variable, "$succeed" to equal, "true" because it seems that no matter what "$SCREEN_CHECK_STATUS" equals, (1 or 0) the if statement won't do it's thing.

Thank you.

## FUNCTIONS

FUNCTION_SERVER_START() 
{
  FUNCTION_SCREEN_CHECK
  echo "debug: $SCREEN_CHECK_STATUS"

debug: 0

  if [ "$SCREEN_CHECK_STATUS" -eq 1 ]
  then
  {
    echo "Starting the server with option 0."
    command_that_starts_server_opt_0
    succeed="true"
  }
  else
  {
    echo "Starting the server with option 1"
    command_that_starts_server_opt_1
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "debug: Succeeded!"
  }
  else
  {
    echo "debug: Failed!"

debug: Failed!

  }
  fi
}

FUNCTION_SCREEN_CHECK()
## sees if the screen session is running already. if it is, $SCREEN_CHECK_STATUS=1, if not, it's 0 by default
{
  SCREEN_CHECK_STATUS="0"
  if [ "$(screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})" ]  ## runs, "screen -ls <screenname> then filters with grep. basically sees if screen session is already running.
  then
  {
    SCREEN_CHECK_STATUS="1"
  }
  else
  {
    echo "debug: screen session IS NOT running"; fi
  }
  fi
}  


## RUNNER

FUNCTION_SERVER_START

EDIT:

sh -xv said there were syntax errors on lines that declared an array, so I used bash -xv and got this:

+ FUNCTION_SERVER_START
+ FUNCTION_SCREEN_CHECK
+ SCREEN_CHECK_STATUS=0
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})"
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]})
screen -ls ${SCREEN_NAME[$SERVER_SELECTED]} | grep ${SCREEN_NAME[$SERVER_SELECTED]}
++ grep delphi
++ screen -ls delphi
+ '[' '' ']'
+ echo 'debug: delphi screen session IS NOT running'
debug: delphi screen session IS NOT running
+ '[' 0 -eq 1 ']'
+ '[' '' == true ']'
+ echo 'debug: Failed!'
debug: Failed!

Upvotes: 2

Views: 568

Answers (2)

Chris Grimmett
Chris Grimmett

Reputation: 297

Silly, silly me. I should have posted the full, non-edited script snippet. Only then would the real problem be visible:

FUNCTION_SERVER_START() 
## starts the minecraft server in screen.
## it uses FUNCTION_SCREEN_CHECK to see if a screen session is already running with the server's designated screen name.
## there are two ways of starting the server. one is sending the mc start string (ex: "java -Xmx512 -Xms512 -jar minecraft_server.jar")...
## ...to the page 0 of the screen session already running, and the other way is to execute the same command in a new screen session.
{
  echo "test phase: can a funky call a phunky?"
  FUNCTION_SCREEN_CHECK
  if [ $DEBUG_MODE -eq 1 ]; then echo "debug: \$SCREEN_CHECK_STATUS = $SCREEN_CHECK_STATUS"; fi
  if [ $SCREEN_CHECK_STATUS -eq 1 ]
  then
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is running."; fi
    screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"
    sleep 5
    succeed="true"
  }
  else
  {
    if [ $DEBUG_MODE -eq 1 ]; then echo "Starting the ${SCREEN_NAME[$SERVER_SELECTED]} minecraft server assuming screen is not running."; fi
    screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"
    sleep 5
    succeed="true"
  }
  fi
  if [ "$succeed" == "true" ]
  then
  {
    echo "\$\$\$\$\$\$\$\$\$\$\$\$\$ test phase: passed the func serv start if"
  }
  else
  {
    echo "****** test phase: failed the func serv start if"
  }
  fi
}

Line 14 is missing a doublequote, and Line 21 has an extra doublequote.

Line 14 wrong:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff $(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

Line 14 right:

screen -S ${SCREEN_NAME[$SERVER_SELECTED]} -p 0 -X stuff "$(printf "java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}")"

Line 21 wrong:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}"

Line 21 right:

screen -m -d -S ${SCREEN_NAME[$SERVER_SELECTED]} java -Xmx${SERVER_MEMORY[$SERVER_SELECTED]}M -Xms${SERVER_MEMORY[$SERVER_SELECTED]}M ${SERVER_ARGUMENTS[$SERVER_SELECTED]} -jar ${SERVER_DIR[$SERVER_SELECTED]}${SERVER_JAR_NAME[$SERVER_SELECTED]}

But it was impossible for anyone but me to know that because I didn't post the right code, Arrrgh!

I wrote the script in nano, and only when I brought it into gedit which has syntax highlighting did I see the problems.

Thank you everyone for your comments.

Upvotes: 3

bonkydog
bonkydog

Reputation: 2032

You have a stray fi at the end of this line:

echo "debug: screen session IS NOT running"; fi

You also don't need the curly braces you are putting around your if-else bodies.

Upvotes: 1

Related Questions