如何使用python子进程调用用Shell编写的jq?

测试用户:

我有以下两个Shell脚本。

node.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.shcat ./nodes.sh | bash):

[
  {
    "node": "b2d9g6i9yp5uj5k25h1ehp26e",
    "ip": "192.168.1.123"
  },
  {
    "node": "iy25xmeln0ns7onzg4jaofiwo",
    "ip": "192.168.1.125"
  }
]

node_detail.sh

#!/bin/bash

docker node inspect b2d | jq '.[] | {node: .ID, ip: .Status.Addr}'

node_detail.sh给出的(./node_detail.shcat ./node_detail.sh):

{
  "node": "b2d9g6i9yp5uj5k25h1ehp26e",
  "ip": "192.168.1.123"
}

问题:我想从python运行两个脚本subporcess

我可以使用以下代码运行并获取node_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获取输出

>>> 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

为什么我得到错误:写入输出失败:管道损坏

峰值:

在nodes.sh中,而不是不带任何参数调用jq,而是将其调用为jq -s .

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在iPython Notebook中调用用argparse编写的模块

如何终止以shell = True启动的python子进程

Python:shell = False的子进程调用不起作用

如何从汇编中调用用C编写的代码?

如何通过单个python子进程调用执行多个Shell命令?

使用Python子进程处理交互式Shell

如何从Python子进程的while循环中退出shell?

使用Python子进程执行Shell命令

Python-如何执行shell cmd'last | grep“已登录”'使用子进程。

如何从React组件调用用html根脚本编写的函数

是否可以使用python子进程调用登录shell?

不使用shell选项无法从python子进程调用ubuntu'ulimit'

如何在不使用shell = True的情况下执行此Python子进程调用?

Python子进程调用挂起?

如何使用python编写linux shell命令?

使用子进程调用带有参数的python脚本

对 insmod 的 Python 子进程调用产生未定义的符号错误,它从 std shell 工作

在python子进程模块中调用adb时如何防止后台进程的新实例

如何使用用户输入使用 Tkinter 编写输出?

如何在python3中使用子进程编写命令

从python的子进程连续调用shell命令并解析输出

在for循环中使用变量调用Python子进程

如何使用python子进程模块执行多个shell命令?

如何使用 Python 子进程捕获子进程的错误?

如何从 SwiftUI 按钮调用用 UIKit/UIController/Storyboard 编写的函数

如何使用子进程执行shell命令?

如何在python中捕获子进程调用异常?

如何使用子进程调用 python3 克服类型错误

如何将shell函数作为子进程调用?