簡體   English   中英

將帶空格的命令字符串傳遞給 docker-compose exec

[英]Pass a command string with space to docker-compose exec

真實場景:在我的自動化腳本中,有一個 cli 可以在 barman 容器上運行barman recover 但是,可以將"ssh xxx"作為整個字符串參數傳遞給--remote-ssh-command ,並且酒保報告“酒保:錯誤:無法識別的 arguments:postgres@IP -p 60022”

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

問題是,如何通過docker-compose exec傳遞帶引號的字符串 arg?

感覺需要轉義遠程命令兩次(第一個 escaping 用雙引號完成):

#!/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")"

我的解決方案:使用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) 不能很好地處理帶有空間的 arguments。 幸好Go版本(docker compose)沒有這個問題。

我嘗試了@Fravadona 的回答,但不幸的是在 Alpine Linux 中printf沒有%q

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM