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
Without the while + read loop, Something like.
#!/usr/bin/env bash
output=$(
curl "https://theurl.events/logs" 2>&1 |
grep -E 'Error|WrongOperation|WronButton'
)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With