1

I'm not a good Shell Script expert. Can somebody help me to understand and explain the statement [ ! -z ${TRUE:-} ] && [ ${TRUE:-} == 'true' ] &&from the following snippet. What is this specific statement doing and/or being validated for?

However, am able to understand this statement: TRUE=$($JBOSS_TWIDDLE get jboss.system:type=Server Started |grep Started |cut -d"=" -f2). This basically calling a JBoss's JMX command-line tool to check the server start status.

Code Snippet:

while [ $i -lt 180 ]; do
        TRUE=$($JBOSS_TWIDDLE get jboss.system:type=Server Started |grep  Started |cut -d"=" -f2)

        [ ! -z ${TRUE:-} ] && [ ${TRUE:-} == 'true' ] && {
    echo "The $JBOSS_NAME server is up and running."
    return
}
Gnanam
  • 1,459
  • 13
  • 26
  • 32
  • This may help: [ -z STRING ] True of the length if "STRING" is zero. It looks like it's saying if it's not empty and it's true. – Drew Khoury Apr 22 '13 at 05:15

2 Answers2

2

The elaborate precautions against $TRUE being empty are not needed if double quotes are used.

[ "$TRUE" = 'true' ]

The -z tests if $TRUE is not empty, and :- substitutes nothing if it is unset. It is all unnecessary. Perhaps it is a protest against an over-prescriptive coding standard.

ramruma
  • 2,740
  • 1
  • 15
  • 8
2

As @drew-khoury and @ramruma point out, the [ -z ... test is superfluous.

Using TRUE as the name of a variable just adds to the confusion.

The actual question that snippet tries to answer is: did twiddle.sh print Started=true? Or, in shell speak:

jboss_is_started() {
    ($JBOSS_TWIDDLE get jboss.system:type=Server Started |
        grep 'Started=true' | read)
}

The grep ... | read construct will return true if grep was successful, while dropping the output.

if jboss_is_started; then
     echo "The $JBOSS_NAME server is up and running."
     return
fi
Henk Langeveld
  • 1,314
  • 10
  • 25