bash - docker-machine ssh尝试在本地机器上运行部分命令

标签 bash docker docker-machine

运行以下命令时:

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/

相关文章:

linux - 我需要在一行中打印多个结果

docker - 了解 docker diff

docker - docker-machine中的端口转发?

linux - 在终端中使用 vim 并使用单独的选项卡创建命令来打开 *.h 文件

Bash 脚本 - 确定脚本是在终端控制台还是在 gui 中启动

bash - 使用别名更改 scp 命令

postgresql - docker ,错误: cannot run migrations: database needs bootstrapping;

node.js - 无法访问 Vagrant Sync 文件夹内的文件

ssh - 使用 docker-machine ssh 在远程机器上运行交互式本地脚本

amazon-ec2 - docker-machine 生成的实例在 AWS 中的位置