运行以下命令时:
docker-machine ssh name-here "docker swarm init --advertise-addr $(hostname -I | awk '{print $1}')"
我收到以下错误:
hostname: illegal option -- I usage: hostname [-fs] [name-of-host]
这是因为该命令正在尝试在没有
hostname -I
标志的MacOS上运行的本地计算机上运行-I
。尝试不带双引号运行会出现相同的错误。
我将full命令封装在双引号中,因此我的期望是双引号的全部内容作为
docker-machine ssh
的单个参数执行,但似乎并非如此。我已经尝试过使用单引号(
'
),但这会干扰awk围绕打印的单引号要求。我什至尝试了疯狂的$$
,因为您将在主机名旁边的makefile中使用它,但这也不起作用。当命令用双引号引起来时,为什么在主机上执行
$(hostname -I | awk '{print $1}')
?如何正确运行以上命令以在主机内初始化具有主机名的docker swarm?
最佳答案
根据man bash
:
Enclosing characters in double quotes preserves the literal value of all characters within the quotes, with the exception of $,
, \, and, when history expansion is enabled, !. The characters $ and
retain their special meaning within double quotes. The backslash retains its special meaning only when followed by one of the following characters: $, `, ", \, or . A double quote may be quoted within double quotes by preceding it with a backslash. If enabled, history expansion will be performed unless an ! appearing in double quotes is escaped using a backslash. The backslash preceding the ! is not removed.
因此,请牢记以下内容:
docker-machine ssh name-here 'docker swarm init --advertise-addr $(hostname -I | awk '\'{print \$1}\'')'
请注意,如何使用
$1
对输出中的\
进行转义,并且还在打印周围对一组单引号进行转义。
关于bash - docker-machine ssh尝试在本地机器上运行部分命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60730944/