Regarding usage of HOSTNAME in rabbitmq's bash script

bash, erlang, rabbitmq

Solution

This expressions checks if `HOSTNAME` is unset:

[ "x" = "x$HOSTNAME" ]

If `HOSTNAME` is unset, this ends up looking like:

[ "x" = "x" ]

Which of course evaluates to `true`. The expression:

[ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname`

Will set `HOSTNAME` to the output of `env hostname` if the expression before `&&` is `true`. Calling `env hostname` is exactly identical to just calling `hostname`, which simply outputs the name of the local host.

The second expression:

NODENAME=rabbit@${HOSTNAME%%.*}

Is using `bash` variable expansion to strip off everything but the first component of the hostname. Given `HOSTNAME="host.example.com"`, `${HOSTNAME%%.*}` returns `host`. You can read more in the `bash` man page:

${parameter%%word}
  Remove matching suffix pattern.  The word is expanded to produce
  a pattern just as in pathname expansion.  If the pattern matches
  a trailing portion  of  the expanded value of parameter, then
  the result of the expansion is the expanded value of parameter
  with the shortest matching pattern (the ``%'' case) or the longest 
  matching pattern (the ``%%'' case) deleted.

So this sets `NODENAME` to be `rabbit@host`, assuming that your local hostname is `host.example.com`.

Problem

In rabbitmq script file of "rabbitmq-env", there is following lines. ``` [ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname` NODENAME=rabbit@${HOSTNAME%%.*} ``` What's the meaning of the first line? Is it to check `$HOSTNAME` is set or not, if not set, set to `'env hostname'`? It is the first line's programming pattern that occupies the most part of another related script file `"rabbitmq-server"`. So I want to know the clear meaning of this line. For the second line, what's the meaning of `HOSTNAME%%.*`?

Original source