[英]How to pass a command with $() to exec.command() in golang
I want to execute a command like docker exec "$(docker-compose ps -q web)" start.sh
from golang script using exec.command()
. 我想使用
exec.command()
从golang脚本执行类似docker exec "$(docker-compose ps -q web)" start.sh
的命令。 The problem is getting the command inside $()
to execute. 问题是让命令在
$()
中执行。
The command inside of $()
is executed and replaced with its output by your shell on the command line (typically bash
but can be sh
or others). $()
内部的命令已执行,并由命令行中的shell替换为输出(通常为bash
但可以为sh
或其他)。 exec.Command is running the program directly, so that replacement isn't happening. exec.Command直接运行该程序,因此不会发生替换。 This means you need to pass that command into bash so it will interpret and execute the command:
这意味着您需要将该命令传递到bash中,以便它将解释并执行该命令:
bash -c "docker exec \\"$(docker-compose ps -q web)\\" start.sh"
Code Example: 代码示例:
exec.Command("/bin/sh", "-c", "docker exec \\"$(docker-compose ps -q web)\\" start.sh")
Alternatively, you can run docker-compose ps -q web
yourself, get its output and do the substitution instead of having bash do it for you. 或者,您可以自己运行
docker-compose ps -q web
,获取其输出并进行替换,而无需bash替您完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.