Home > database >  curl stream output read line by line and contains conditions
curl stream output read line by line and contains conditions

Time:09-17

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

CodePudding user response:

Without the while read loop, Something like.

#!/usr/bin/env bash

output=$(
  curl "https://theurl.events/logs" |
  grep -E 'Error|WrongOperation|WronButton' 2>&1
)

printf '%s\n' "$output"

if [[ $output =~ Error ]]; then
  echo ./runbash1.sh
elif [[ $output =~ WrongOperation ]]; then
  echo ./runbash2.sh
elif [[ $output =~ WronButton ]]; then
  echo ./runbash3.sh
fi

Remove the echo's if you're satisfied with the output.

CodePudding user response:

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.

CodePudding user response:

Consider this approach:

#!/bin/bash

events=(
    # Event type   |  Action
    'error          ./runbash1.sh'
    'wrongoperation ./runbash2.sh'
    'wrongbutton    ./runbash3.sh'
)

data=$(curl "https://theurl.events/logs")

for event in "${events[@]}"; do
    read     type action <<<  $event
    grep -i $type        <<< "$data" && $action
done

Without grep:

[[ $data =~ $type ]] && $action
  • Related