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
}
1
  • 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. Apr 22, 2013 at 5:15

2 Answers 2

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
1
  • Yes, you're right. twiddle.sh prints Started=true.
    – Gnanam
    Apr 23, 2013 at 5:13
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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .