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!
sh -xv?bash -xv[[and]]. It's a bashism but it works nicely as long as it's a bash script.