Thursday, February 3, 2022

[SOLVED] curl stream output read line by line and contains conditions

Issue

I have an API url that allows to monitor some events. I am able to do it with a simple code curl "https://theurl.events/logs" all the logs are in text format, it never ends, so I just run curl command and leave it there.

Now I want to put a some conditions if the log contains a keyword then do something.

The log looks like below, it looks like json but it is text not json

action=machinestarted,
data={
    "location": "Place A"
    "lag": "033"
    "size": "5543"
    "id": "11",
    .....
}
action=Error,
data={
    "location": "Place B"
    "lag": "033"
    "size": "5543"
    "id": "11",
    .....
}

so far I can filter the logs doing curl "https://theurl.events/logs" 2>&1 | grep Error | ./runbash.sh

since the events grow, I wanted to grep more keywords eg. grep WrongOperation, grep WrongButton then I want run different bash file.

I don't think it is a good idea to run them separately e.g

"https://theurl.events/logs" 2>&1 | grep Error` | ./runbash1.sh
"https://theurl.events/logs" 2>&1 | grep WrongOperation` | ./runbash2.sh
"https://theurl.events/logs" 2>&1 | grep WrongButton` | ./runbash3.sh

so I'd like to know if it is possible to use while loop the output from curl and contains multiple conditions, something like

while IFS= read -r line (from curl)
do
  if [[ "$line" == *"WrongOperation"* ]]; then
    //do something
   elif
    [[ "$line" == *"WrongButton"* ]]
    //.....
done

Solution

You could rely on the following approach:

while IFS= read -r line
do
  if [[ "$line" == *"WrongOperation"* ]]; then
    //do something
   elif
    [[ "$line" == *"WrongButton"* ]]
    //.....
done < $(YOUR_CURL_COMMAND_LINE)

Replace YOUR_CURL_COMMAND_LINE with your curl command.

Here more information on doing that.

Regards.



Answered By - Antonio Petricca
Answer Checked By - Candace Johnson (WPSolving Volunteer)