You are not logged in.
Obviously my bash-foo suffered over the years.
I am trying to read the output of a command line-by-line and return if a line contains a pattern.
Here's the MRE of what I'm trying to achieve. Feel free to replace $NEEDLE as needed.
#! /bin/bash
NEEDLE="Cannot find any crtc or sizes"
dmesg --follow | while IFS= read -r LINE; do
if [[ "${LINE}" == *"${NEEDLE}"* ]]; then
echo "Found: ${LINE}";
exit 0;
else
echo "No match: ${LINE}";
fi
done
The problem is, that this script does not exit when the pattern has been found.
I assume it's somehow because it's still waiting for the pipe from dmesg to close.
Is there a way to force the script to terminate, once the pattern has been found?
Last edited by schard (2025-07-05 11:32:51)
Inofficial first vice president of the Rust Evangelism Strike Force
Offline
You're exiting the subshell created by the pipe
while … do; … done < <(dmesg -W)
dmesg -W | grep --line-buffered -m1 "$NEEDLE" && exit
does not work?
Edit: missed a shift key
Last edited by seth (2025-07-04 06:06:22)
Online
The 'echo '"No match: ${LINE}"' is always run as long as there are 'No match' found before match 'Found'
You can leave the while loop with 'break'
Why not remove 'No match' from the equation.
I mean if everything is good, do you really need that message
Edit:so I forget to paste the code...
#!/bin/bash
NEEDLE="RSDP"
sudo dmesg --follow | while IFS= read -r LINE; do
if [[ "${LINE}" == *"${NEEDLE}"* ]]; then
echo "Found: ${LINE}"
break
fi
done
To make it into a more useful tool replace 'NEEDLE' variable with:
printf '%s\n' "What line are you looking for"
read -r NEEDLE
Last edited by qinohe (2025-07-04 10:43:51)
Offline
Do you need to save matched line? If not, the script can be simplified to
dmesg --follow | grep -q "$NEEDLE"
or
dmesg --follow | sed "/$NEEDLE/q"
with all power of grep or sed options.
Offline
As a very different approach, you could use the -J flag for dmesg to get json output and pipe it to jq for search/filtering and formatting results' print out.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thank you all.
No, I don't actually need the output.
The script's purpose is to keep searching dmesg until a pattern is found and then terminate.
Its purpose is to operate in a systemd waiting service for certain kernel messages so that other services can be started after certain events have been logged.
@Seth
This works perfectly fine and solves my issue. Thank you.
Last edited by schard (2025-07-05 11:33:48)
Inofficial first vice president of the Rust Evangelism Strike Force
Offline