0

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.

1
  • && 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 Commented Aug 24, 2013 at 4:22

1 Answer 1

1

&& 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).

You must log in to answer this question.

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