I have a script that outputs about 10 lines every time if it run. The content of these lines varies.
I would really like to be able to grep in the output and do different things depending on the output.
In pseudo this is what I would like to do
cat /etc/password | \\
if [ grep "root" $STDOUT ]; then
echo "root is found"
elif [ grep "nobody" $STDOUT ]; then
echo "nobody is found"
fi
Here have I used cat /etc/password as an example, but it should be replaced with my scripts mentioned above.
The problem is, how do I get hold of the output from cat /etc/password in the if/elif conditions?
As @Benoit recommends, just use
grepdirectly.As @larsmans notes, you can avoid a double-read of the file by reading it into a variable once.
Given the availability of
bashI’d do it like this:One read of the file, one or two invocations of
grep, no other processes or subshells launched.