Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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
}
share|improve this question
    
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 5:15
add comment

2 Answers

up vote 2 down vote accepted

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
share|improve this answer
    
Yes, you're right. twiddle.sh prints Started=true. –  Gnanam Apr 23 '13 at 5:13
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.