2

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!
5
  • Are you getting any debug output from the script? Commented Oct 23, 2011 at 8:21
  • can you run it with sh -xv ? Commented Oct 23, 2011 at 8:29
  • In your debug output, is $SCREEN_CHECK_STATUS coming out as 0,1 or nothing? Can you edit your post and include the output that you do get when you run this script? Commented Oct 23, 2011 at 8:30
  • Yes, I am getting debug output, I edited my post to show the two lines of output the script produces, and the output of bash -xv Commented Oct 23, 2011 at 17:10
  • Try double [[ and ]]. It's a bashism but it works nicely as long as it's a bash script. Commented Oct 23, 2011 at 19:30

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

2 Comments

Yeah, I messed up when simplifying the code in the original post (big mistake!) The original line looked like this: if [ $DEBUG_MODE -eq 1 ]; then echo "debug: screen session IS NOT running"; fi Are curly braces around if-else bodies required if there are multiple commands inside? Ex: if x then { cmd1 cmd2 cmd3 } fi
no, curly braces are not required, even when multiple cmds. You can use curl braces to group output, like {printf "xxx" \n" ; printf "yyy" "\n" ; } | grep y. A pretty lame example, but a very handy technique for advanced shell programming. Good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.