在shell脚本上操作输出

在shell脚本上操作输出,shell,Shell,我有一些输出数据,我只想把一个值带入一个变量。输出如下: Vin Script Sig: Vin Tx hash:0000000000000000000000000000000000000000000000000000000000000000 error: {"code":-1,"message":"The message"} #!/bin/bash string="command to generate the output" code=$string "some kind of filte

我有一些输出数据,我只想把一个值带入一个变量。输出如下:

Vin Script Sig:
Vin Tx hash:0000000000000000000000000000000000000000000000000000000000000000
error: {"code":-1,"message":"The message"}
#!/bin/bash
string="command to generate the output"
code=$string "some kind of filters"

echo $code
我只想把“code”中的值放入一个变量中。在这种情况下,变量中的值为-1。我在想这样的剧本:

Vin Script Sig:
Vin Tx hash:0000000000000000000000000000000000000000000000000000000000000000
error: {"code":-1,"message":"The message"}
#!/bin/bash
string="command to generate the output"
code=$string "some kind of filters"

echo $code

响应似乎是JSON,但包含大量文本。理想情况下,您可以访问仅JSON的部分,然后使用JQ查询/操作它。对于您提供的特定情况,shell可以这样做。假设输出捕获在文件“result file.txt”中

code=
while read p ; do
     if [[ "$p" == error:* ]] ; then
         code=${p#*\"code\":};
         code=${code%,*};
         break
     fi;
done <result-file.txt
code=
读p;做
如果[[“$p”==错误:*];然后
代码=${p\*\'code\':};
代码=${code%,*};
打破
fi;
完成