变量替换在 shell 脚本中不起作用

问题描述 投票:0回答:2

我有一个简单的 shell 脚本。我正在使用 jq 来解析一些 json 数据。但是它不起作用。所以以下是我的shell脚本

#! /bin/sh

cat sample.json  | jq '. | select(.type=="Point" and .metric=="iterations") | .data.value' | jq -s add


COUNT=`cat sample.json`  | jq '. | select(.type=="Point" and .metric=="iterations") | .data.value' | jq -s add

echo "Value of Count: $Count"

TOTAL=$(cat sample.json)


COUNT_1=`$TOTAL | jq '. | select(.type=="Point" and .metric=="iterations") |.data.value' | jq -s add`

echo "Value of Count_1: $Count_1"

我的脚本生成的输出如下

1
null
Value of Count:
test.sh: line 13: {"type":"Metric","data":{"name":"http_reqs","type":"counter","contains":"default","thresholds":[],"submetrics":null},"metric":"http_reqs"}: command not found
Value of Count_1:

我可以看到变量 COUNT_1 和 COUNT 没有正确打印。我希望他们有价值 1

这是我在这个问题上使用的

sample.json


{"type":"Metric","data":{"name":"http_reqs","type":"counter","contains":"default","thresholds":[],"submetrics":null},"metric":"http_reqs"} {"metric":"http_reqs","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":1,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_duration","type":"trend","contains":"time","thresholds":[],"submetrics":[{"name":"http_req_duration{expected_response:true}","suffix":"expected_response:true","tags":{"expected_response":"true"}}]},"metric":"http_req_duration"} {"metric":"http_req_duration","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":62.062662,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_blocked","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_blocked"} {"metric":"http_req_blocked","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":1.246508,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_connecting","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_connecting"} {"metric":"http_req_connecting","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":1.072056,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_tls_handshaking","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_tls_handshaking"} {"metric":"http_req_tls_handshaking","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":0,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_sending","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_sending"} {"metric":"http_req_sending","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":0.319471,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_waiting","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_waiting"} {"metric":"http_req_waiting","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":60.315417,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_receiving","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"http_req_receiving"} {"metric":"http_req_receiving","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":1.427774,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"http_req_failed","type":"rate","contains":"default","thresholds":[],"submetrics":null},"metric":"http_req_failed"} {"metric":"http_req_failed","type":"Point","data":{"time":"2023-02-22T02:07:16.696166854Z","value":0,"tags":{"expected_response":"true","group":"","method":"GET","name":"http://172.19.0.4:30339/SccTestService/v1/id","proto":"HTTP/1.1","scenario":"default","status":"200","url":"http://172.19.0.4:30339/SccTestService/v1/id"}}} {"type":"Metric","data":{"name":"data_sent","type":"counter","contains":"data","thresholds":[],"submetrics":null},"metric":"data_sent"} {"metric":"data_sent","type":"Point","data":{"time":"2023-02-22T02:07:16.696387685Z","value":102,"tags":{"group":"","scenario":"default"}}} {"type":"Metric","data":{"name":"data_received","type":"counter","contains":"data","thresholds":[],"submetrics":null},"metric":"data_received"} {"metric":"data_received","type":"Point","data":{"time":"2023-02-22T02:07:16.696387685Z","value":266,"tags":{"group":"","scenario":"default"}}} {"type":"Metric","data":{"name":"iteration_duration","type":"trend","contains":"time","thresholds":[],"submetrics":null},"metric":"iteration_duration"} {"metric":"iteration_duration","type":"Point","data":{"time":"2023-02-22T02:07:16.696387685Z","value":63.639606,"tags":{"group":"","scenario":"default"}}} {"type":"Metric","data":{"name":"iterations","type":"counter","contains":"default","thresholds":[],"submetrics":null},"metric":"iterations"} {"metric":"iterations","type":"Point","data":{"time":"2023-02-22T02:07:16.696387685Z","value":1,"tags":{"group":"","scenario":"default"}}}

bash
2个回答
1
投票

您的代码有几个问题,包括印刷错误

如果你的意思是
/bin/bash
,请使用
/bin/bash

#!/bin/bash

使用 $() 而不是 `` 并包含您想要捕获标准输出的整个内容:

COUNT=$(cat sample.json  | jq '. | select(.type=="Point" and .metric=="iterations") | .data.value' | jq -s add)

变量区分大小写(
$COUNT
不是
$Count

echo "Value of Count:
$Count"
$COUNT

印刷错误

请注意shell中变量的大小写敏感

echo "Value of Count_1: $Count_1"

不会打印任何内容,因为

$COUNT_1
$Count_1

不同

更正脚本


#!/bin/bash

cat sample.json | jq '. 
    | select(.type=="Point" and .metric=="iterations") 
    | [ .data.value ] | flatten | add
   ' 

你不需要使用

jq -s add
当然 jq 将一个文件作为输入,但你可能正在将它作为另一个进程的输出,所以我把
cat sample.json
留在那里,我认为它是管道在其他东西中的占位符。如果没有,那么您可以通过

进一步简化

#!/bin/bash

jq '. 
    | select(.type=="Point" and .metric=="iterations") 
    | [ .data.value ] | flatten | add
   ' sample.json

0
投票

如果要将管道的输出分配给变量,则必须将整个管道包含在反引号中,而不仅仅是它的第一部分。

COUNT=`cat sample.json | jq '. | select(.type=="Point" and .metric=="iterations") | .data.value' | jq -s add`
#                     ~                                                                                     ~

此外,最好使用

$(...)
而不是
`...`
,因为它们可以正确嵌套。

此外,不需要使用

cat
jq
可以将文件名作为参数,也可以使用重定向。

最后,变量不要使用大写。它应该只用于系统变量。

count=$(< sample.json  jq '. | select(.type=="Point" and .metric=="iterations") | .data.value' | jq -s add)
© www.soinside.com 2019 - 2024. All rights reserved.