提问人:silencej 提问时间:4/18/2022 更新时间:4/19/2022 访问量:883
将带有空格的命令字符串传递给 docker-compose exec
Pass a command string with space to docker-compose exec
问:
现实生活场景:在我的自动化脚本中,有一个 cli 可以在 barman 容器上运行。但是,可以将整个字符串 arg 传递给 ,并且 barman 报告“barman: error: unrecognized arguments: postgres@IP -p 60022”barman recover
"ssh xxx"
--remote-ssh-command
docker-compose exec -T barman barman recover SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming --remote-ssh-command "ssh postgres@IP -p 60022"
# I tried with bach -c trick, still no luck.
docker-compose exec -T barman bash -c "barman recover SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming --remote-ssh-command 'ssh postgres@IP -p 60022'"
为了简化起见,想象一个最小的重现案例:
docker-compose exec -T barman bash -c "echo hello"
# (It echos nothing. But I want it to behave like the following.)
docker-compose exec -T barman echo hello
# hello
问题是,如何传递带引号的字符串 arg ?docker-compose exec
答:
2赞
Fravadona
4/19/2022
#1
感觉您需要转义远程命令两次(第一次转义是用双引号完成的):
#!/bin/bash
docker-compose exec -T barman \
barman recover \
SRV1 last /var/lib/postgresql/data/pgdata/recovered.streaming \
--remote-ssh-command "$(printf %q "ssh postgres@IP -p 60022")"
评论
0赞
silencej
4/19/2022
谢谢你的回答。它可能适用于 BASH,但遗憾的是我使用 Alpine Linux 作为 Docker 基础映像,因此不支持 %q。
2赞
silencej
4/19/2022
#2
我的解决方案:使用而不是.docker compose
docker-compose
docker compose exec -T barman bash -c "echo hello"
# hello
docker-compose exec -T barman echo hello
# hello
我相信 Python 版本(docker-compose)不能很好地处理带有空格的参数。幸运的是,Go 版本(docker compose)没有这样的问题。
我尝试了@Fravadona的答案,但不幸的是,在 Alpine Linux 中没有.printf
%q
评论