运行 带有变量的 bash 命令作为 python 子进程

Run a bash command with variables as a python subprocess

我有这个 shell 命令:

$ docker run -it --env-file=.env -e "CONFIG=$(cat /path/to/your/config.json | jq -r tostring)" algolia/docsearch-scraper

我想 运行 它作为 python 子进程。 我以为我只需要 jq -r tostring 的等效项,但如果我将 config.json 用作普通字符串,则 " 不会被转义。我还使用 json.load 对它们进行了转义(config.json)。 使用原始 jq 命令, " 也不会被转义,它只是返回 json 字符串。

当我在 python 子进程中使用作为字符串返回的 json 时,我总是在子进程行上得到 FileNotFoundError。

@main.command()
def algolia_scrape():
    with open(f"{WORKING_DIR}/conf_dev.json") as conf:
        CONFIG = json.load(conf)
        subprocess.Popen(f'/usr/local/bin/docker -it --env-file={WORKING_DIR}/algolia.env -e "CONFIG={json.dumps(CONFIG)}" algolia/docsearch-scraper')

你得到“找不到文件”是因为(没有 shell=True)当你想要 运行 [时,你正在尝试 运行 一个名称为 /usr/local/bin/docker -it ... 的命令 /usr/local/bin/docker 带有一些参数。当然,尝试将 JSON 传递给 shell 几乎是一场噩梦,因为您需要从字符串中转义任何 shell 元字符;但只需将命令字符串分解为字符串列表,就像 shell 那样。

def algolia_scrape():
    with open(f"{WORKING_DIR}/conf_dev.json") as conf:
        CONFIG = json.load(conf)
    p = subprocess.Popen(['/usr/local/bin/docker', '-it',
        f'--env-file={WORKING_DIR}/algolia.env',
        '-e', f'CONFIG={json.dumps(CONFIG)}',
        'algolia/docsearch-scraper'])

您通常希望保存 subprocess.Popen() 的结果,因为当进程终止时您需要 wait