如何执行 url 并从 bash shell 脚本解析它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22235637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:49:13  来源:igfitidea点击:

How to execute a url and parse it from bash shell script?

linuxbashshellunixubuntu

提问by

I am working on a project in which I need to make a url call to one of my server from the bash shell script..

我正在开发一个项目,在该项目中我需要从 bash shell 脚本对我的一台服务器进行 url 调用。

http://hostname.domain.com:8080/beat

After hitting the above url, I will be getting the below response which I need to parse it and extract value of syncsand syncs_behind

点击上面的 url 后,我将得到以下响应,我需要解析它并提取syncssyncs_behind

state: READY num_retries_allowed: 3 syncs: 30 syncs_behind: 100 num_rounds: 60 hour_col: 2 day_col: 0 oldest_day_col: 0

Now I need to hit the above url every 10 seconds for a period of 10 minutes and extract the value of syncsand syncs_behindfrom it and use to validate it with below condition -

现在,我需要打上面的网址每10秒一个10分钟的时间和提取物的价值syncs,并syncs_behind从它和使用下面的条件来验证它-

syncs > 8
syncs_behind = 0

if the syncs is greater than 8 and syncs_behind = 0, then I will end my shell script with some message saying - "Data has been validated", otherwise I would keep on trying for 10 minute window.. If in that 10 minute window, this doesn't happen I will end the shell script anyway meaning I won't retry again.

如果同步大于 8 并且 syncs_behind = 0,那么我将用一些消息结束我的 shell 脚本 - “数据已被验证”,否则我会继续尝试 10 分钟窗口。如果在那个 10 分钟窗口中,这不会发生,无论如何我都会结束 shell 脚本,这意味着我不会再试一次。

So I started with the below code but got stuck, what should I do to parse the data coming from the URL -

所以我从下面的代码开始,但卡住了,我该怎么做才能解析来自 URL 的数据 -

#!/bin/sh
wget -O - -q -t 1 http://hostname.domain.com:8080/beat

I am not familiar with shell script that much so after reading it I came to know about wget.. There might be some better way of doing it..

我对 shell 脚本不太熟悉,所以在阅读它之后我开始了解 wget .. 可能有一些更好的方法来做到这一点..

Any thoughts how this can be done?

任何想法如何做到这一点?

UPDATE:-

更新:-

I saved the file as beat.shwith the below contents –

我将文件保存为beat.sh以下内容 -

#!/bin/bash

COUNT=60   #number of 10 second timeouts in 10 minutes
SUM_SYNCS=0
SUM_SYNCS_BEHIND=0

while [[ $COUNT -ge "0" ]]; do

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for syncs and syncs_behind
SYNCS=$(echo $DATA | grep -o 'syncs:: [0-9]+' | awk '{print }')
SYNCS_BEHIND=$(echo $DATA | grep -o 'syncs_behind: [0-9]+' | awk '{print }')
echo $SYNCS
echo $SYNCS_BEHIND

#add new values to the sum totals
let SUM_SYNCS+=SYNCS
let SUM_SYNCS_BEHIND+=SYNCS_BEHIND

#verify conditionals
if [[ $SYNCS -gt "8" -a $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

And when I am running it as ./beat.sh, I got below error -

当我运行它时./beat.sh,出现以下错误 -

./beat.sh: line 23: syntax error in conditional expression
./beat.sh: line 23: syntax error near `-a'
./beat.sh: line 23: `if [[ $SYNCS -gt "8" -a $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi'

Any thoughts what wrong I am doing here?

任何想法我在这里做错了什么?

采纳答案by problemPotato

great start! Let's break it down:

很好的开始!让我们分解一下:

COUNT=60   #number of 10 second timeouts in 10 minutes
SUM_SYNCS=0
SUM_SYNCS_BEHIND=0

while [[ $COUNT -ge "0" ]]; do

#send the request, put response in variable
DATA=$(wget -O - -q -t 1 http://hostname.domain.com:8080/beat)

#grep $DATA for syncs and syncs_behind
SYNCS=$(echo $DATA | grep -oE 'syncs: [0-9]+' | awk '{print }')
SYNCS_BEHIND=$(echo $DATA | grep -oE 'syncs_behind: [0-9]+' | awk '{print }')

#add new values to the sum totals
let SUM_SYNCS+=SYNCS
let SUM_SYNCS_BEHIND+=SYNCS_BEHIND

#verify conditionals
if [[ $SYNCS -gt "8" && $SYNCS_BEHIND -eq "0" ]]; then exit -1; fi

#decrement the counter
let COUNT-=1

#wait another 10 seconds
sleep 10

done

回答by jan groth

Not a c&p ready solution, but hopefully something to get you started:

不是 c&p 就绪的解决方案,但希望可以帮助您入门:

You want to redirect the output of wgetinto a file, and then use a combination of awkand sedto extract the parts that you are actually interested in. You'll probably need a few minutes and 'hello worlds' with both commands, but it well worth the effort.

要重定向输出wget到一个文件,然后使用一个组合awk,并sed提取部分你实际上是在有意。你可能需要几分钟,“你好世界”与这两个命令,但它非常值得努力。

In terms of retrying / exiting, you probably need an endless loop with an ifstatements that evaluates the exit conditions. For time control I'd play with the sleepcommand, even though crontabmight be an alternative worthwhile considering.

在重试/退出方面,您可能需要一个带有if评估退出条件的语句的无限循环。对于时间控制,我会使用sleep命令,即使crontab可能是值得考虑的替代方法。

I'd recommend to focus on the first part (parsing and evaluating) and maybe raise a new question once you're ready to move on...

我建议专注于第一部分(解析和评估),一旦你准备好继续前进,可能会提出一个新问题......