【问题标题】:How to write file from sh如何从sh写入文件
【发布时间】:2013-04-20 06:48:03
【问题描述】:

我有一个我喜欢在 python 中通过 subprocess 执行的脚本(是的,它必须在 sh 中)。 现在我像这样打电话给sh

subprocess.check_call( ['sh' + command] )

command 在哪里:

echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile

不幸的是,这给了我:

sh: 0: Can't open echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile

有人可以指导我完成使命令在sh 中工作的步骤并解释原因。

【问题讨论】:

    标签: python shell sh


    【解决方案1】:

    您必须使用 -c 参数运行 sh:

    subprocess.check_call( ['sh', '-c', command] )
    

    【讨论】:

      【解决方案2】:

      试试这个:

      command = "echo 'someformat : : '${ENV_VAR}'/efc ;' > targetfile"
      subprocess.check_call(["sh", "-c", command])
      

      参数-c 修改sh 行为以从下一个参数的字符串中读取命令。 并且参数必须包含在列表中。

      【讨论】:

        猜你喜欢
        • 2018-09-04
        • 1970-01-01
        • 2012-03-07
        • 2017-06-24
        • 1970-01-01
        • 2014-04-21
        • 1970-01-01
        • 2021-07-17
        • 1970-01-01
        相关资源
        最近更新 更多