Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I am trying to understand the tomcat daemon.sh script which contains a bunch of lines along the lines test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat

I figured out that test ".$TOMCAT_USER" = . && TOMCAT_USER=tomcat checks if an environment variable TOMCAT_USER is already defined if so it uses it and if not it uses the default value of tomcat

What I understand is the how the statement is evaluated by bash. I am a bash scripting noob. I am looking for a step by step explanation of how this statement works.

share|improve this question
    
&& it's Boolean, means if first statement is successfully run (means exit status code is zero ) then run another statement. you can run script in debug mode to understand like bash -x scriptname –  Rahul Patil Aug 24 '13 at 4:22
add comment

1 Answer 1

up vote 1 down vote accepted

&& is a logical "and"; essentially, it lets you do something based on whether the previous command completed successfully (although this might not be true in more complex cases, for example, consider true || false && echo true, here's an article on on boolean logic rules). Another way of writing the statement in your question would be:

if test ".$TOMCAT_USER" = .; then
    TOMCAT_USER=tomcat
fi

This is a strange way of testing for an unset/null variable, though. POSIX already has multiple ways to do that:

: "${TOMCAT_USER:=tomcat}"  # Assign as part of the expansion,
                            # `:' is a noop to avoid running the expansion.
TOMCAT_USER="${TOMCAT_USER:-tomcat}"  # Expand, and then assign

The difference between the :=/:- and :-less versions are that the : versions also test if the variable is null in addition to testing if it is unset (which is closer to your original test).

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.