关于在rabbitmq的bash脚本中使用HOSTNAME

Che*_* Yu 3 bash erlang rabbitmq

在"rabbitmq-env"的rabbitmq脚本文件中,有以下行.

[ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname`
NODENAME=rabbit@${HOSTNAME%%.*}
Run Code Online (Sandbox Code Playgroud)

第一行的含义是什么?是否$HOSTNAME设置检查,如果未设置,设置为'env hostname'

它是第一行的编程模式,占据了另一个相关脚本文件的大部分"rabbitmq-server".所以我想知道这条线的明确含义.

对于第二行,这是什么意思HOSTNAME%%.*

lar*_*sks 6

此表达式检查是否HOSTNAME未设置:

[ "x" = "x$HOSTNAME" ]
Run Code Online (Sandbox Code Playgroud)

如果HOSTNAME未设置,最终看起来像:

[ "x" = "x" ]
Run Code Online (Sandbox Code Playgroud)

当然评估为true.表达方式:

[ "x" = "x$HOSTNAME" ] && HOSTNAME=`env hostname`
Run Code Online (Sandbox Code Playgroud)

将设置HOSTNAME到输出env hostname,如果表达式之前&&true.调用env hostname与仅调用完全相同hostname,只是输出本地主机的名称.

第二个表达式:

NODENAME=rabbit@${HOSTNAME%%.*}
Run Code Online (Sandbox Code Playgroud)

使用bash变量扩展除去主机名的第一个组件之外的所有内容.给予HOSTNAME="host.example.com",${HOSTNAME%%.*}回报host.您可以在bash手册页中阅读更多内容:

${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.
Run Code Online (Sandbox Code Playgroud)

所以这NODENAME就是rabbit@host假定您的本地主机名是host.example.com.