Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python子进程调用在shell中编写的jq?_Python_Bash_Subprocess_Jq - Fatal编程技术网

如何使用python子进程调用在shell中编写的jq?

如何使用python子进程调用在shell中编写的jq?,python,bash,subprocess,jq,Python,Bash,Subprocess,Jq,我有以下两个shell脚本 节点。sh: #!/bin/bash NODE_IDs=$(docker node ls --format "{{.ID}}") for NODE_ID in ${NODE_IDs} do docker node inspect $NODE_ID | jq -r '.[] | {node:.ID, ip:.Status.Addr}' done | jq -s nodes.sh给出以下输出(带有/nodes.sh或cat./nodes.s

我有以下两个shell脚本

节点。sh:

#!/bin/bash

NODE_IDs=$(docker node ls --format "{{.ID}}")
for NODE_ID in ${NODE_IDs}
do
    docker node inspect $NODE_ID | jq -r '.[] | {node:.ID, ip:.Status.Addr}'
done | jq -s
nodes.sh给出以下输出(带有
/nodes.sh
cat./nodes.sh | bash
):

节点_detail.sh

#!/bin/bash

docker node inspect b2d | jq '.[] | {node: .ID, ip: .Status.Addr}'
其中as node_detail.sh给出(
/node_detail.sh
cat./node_detail.sh
):

问题:我想从python
子进程运行这两个脚本

我可以使用以下代码运行并获取节点_detail.sh的输出:

>>> import subprocess
>>> proc = subprocess.Popen('./node_detail.sh', stdout=subprocess.PIPE, shell=True)
>>> proc.stdout.read()
'{\n  "node": "b2d9g6i9yp5uj5k25h1ehp26e",\n  "ip": "192.168.1.123"\n}\n'
我编写了以下代码以从nodes.sh获取输出

现在我得到以下错误:

>>> jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]

    jq is a tool for processing JSON inputs, applying the
    given filter to its JSON text inputs and producing the
    filter's results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jq's input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -c     compact instead of pretty-printed output;
     -n     use `null` as the single input value;
     -e     set the exit status code based on the output;
     -s     read (slurp) all inputs into an array; apply filter to it;
     -r     output raw strings, not JSON texts;
     -R     read raw strings, not JSON texts;
     -C     colorize JSON;
     -M     monochrome (don't colorize JSON);
     -S     sort keys of objects on output;
     --tab  use tabs for indentation;
     --arg a v  set variable $a to value <v>;
     --argjson a v  set variable $a to JSON value <v>;
     --slurpfile a f    set variable $a to an array of JSON texts read from <f>;
    See the manpage for more options.
Error: writing output failed: Broken pipe
Error: writing output failed: Broken pipe
jq-命令行JSON处理器[版本1.5-1-a5b5cbe] 用法:jq[选项][文件…] jq是一个处理JSON输入的工具,应用 为其JSON文本输入提供过滤器,并生成 在标准输出上以JSON格式筛选结果。 最简单的过滤器是.,它是标识过滤器, 未经修改地将jq的输入复制到其输出(除了 格式)。 有关更高级的过滤器,请参阅jq(1)手册页(“man jq”) 和/或https://stedolan.github.io/jq 其中一些选择包括: -c紧凑而不是漂亮的打印输出; -n使用'null'作为单个输入值; -e根据输出设置退出状态代码; -s读取(slurp)数组中的所有输入;对其应用过滤器; -r输出原始字符串,而不是JSON文本; -R读取原始字符串,而不是JSON文本; -C着色JSON; -M单色(不着色JSON); -S输出上对象的排序键; --制表符使用制表符进行缩进; --arg a v将变量$a设置为值; --argjson a v将变量$a设置为JSON值; --slurpfile将变量$a设置为从中读取的JSON文本数组; 有关更多选项,请参见手册页。 错误:写入输出失败:管道断开 错误:写入输出失败:管道断开

出现错误的原因:写入输出失败:管道破裂?

在nodes.sh中,不是不带任何参数调用jq,而是作为
jq-s调用它。
在nodes.sh中,不是不带任何参数调用jq,而是作为
jq-s调用它。
对于这样一个简单的任务来说,这似乎太复杂了。你不能直接使用Docker Python库吗?@Paolo我还没有试过。对于这么简单的任务来说,这似乎太复杂了。你不能直接使用Docker Python库吗?@Paolo我还没试过。
>>> import subprocess
>>> proc = subprocess.Popen('./node_detail.sh', stdout=subprocess.PIPE, shell=True)
>>> proc.stdout.read()
'{\n  "node": "b2d9g6i9yp5uj5k25h1ehp26e",\n  "ip": "192.168.1.123"\n}\n'
>>> import subprocess
>>> proc = subprocess.Popen('./nodes.sh', stdout=subprocess.PIPE, shell=True)
>>> jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]

    jq is a tool for processing JSON inputs, applying the
    given filter to its JSON text inputs and producing the
    filter's results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jq's input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -c     compact instead of pretty-printed output;
     -n     use `null` as the single input value;
     -e     set the exit status code based on the output;
     -s     read (slurp) all inputs into an array; apply filter to it;
     -r     output raw strings, not JSON texts;
     -R     read raw strings, not JSON texts;
     -C     colorize JSON;
     -M     monochrome (don't colorize JSON);
     -S     sort keys of objects on output;
     --tab  use tabs for indentation;
     --arg a v  set variable $a to value <v>;
     --argjson a v  set variable $a to JSON value <v>;
     --slurpfile a f    set variable $a to an array of JSON texts read from <f>;
    See the manpage for more options.
Error: writing output failed: Broken pipe
Error: writing output failed: Broken pipe