0

I am trying to follow the advice on How to check if a variable is set in Bash?

So our app is being deployed on AWS ECS cluster using CloudFormation Template, and sometimes we may need to switch on debug mode.

It seems the best way is to introduce an env variable DEBUG_MODE, that when is set to TRUE, the entrypoint.sh script would launch the java app with debug parameters.

Here is my latest attempt, I initially wanted to use && for single IF, but was getting error so trying to isolate it and split it in two:

if [ -z ${DEBUG_MODE+FALSE} ]
then
        echo not debug, not set
else
        if [ $DEBUG_MODE = TRUE ]
        then
                echo debug
        else
                echo not debug, set to $DEBUG_MODE
        fi
fi

Output:

user@hostname:~$ export DEBUG_MODE=
user@hostname:~$ sh test.sh
test.sh: 5: [: =: unexpected operator
not debug, set to

I cant seem to figure this out, how to write it so that:

  1. If DEBUG_MODE is unset, empty, or anything but TRUE, it executes regular java command.
  2. Only if DEBUG_MODE is TRUE, it executes java command with debug parameters?

Thanks!

Carmageddon
  • 2,627
  • 4
  • 36
  • 56

1 Answers1

0

you could simplify a bit that script without the need to check first if it's empty:

#!/bin/bash

if [[ "${DEBUG_MODE}" == 'TRUE' ]]; then
   # Run in debug mode
else
   # Run as usual
fi

Also I noticed you are using sh this solutions applies to bash, but isn't your Docker image already has bash installed?

In case you need sh only, problem seems to be in the comparison, you might need to wrap your variables with double quotes if [ "$DEBUG_MODE" = TRUE ]

if [ -z "${DEBUG_MODE+FALSE}" ]
then
        echo not debug, not set
else
        if [ "$DEBUG_MODE" = TRUE ]
        then
                echo debug
        else
                echo not debug, set to $DEBUG_MODE
        fi
fi
Sergio Guillen Mantilla
  • 1,460
  • 1
  • 13
  • 20