Eyal
Eyal

Reputation: 1709

Constantly Checking The Output From A Bash Command

I have a script which calls an infinite command which outputs data and will never stop. I want to check if that command outputs a specific string, say "Success!", and then I'd like to stop it and move to the next line on my script.

How is this possible?

Thanks in advance.

Edit:

#!/bin/bash

xrandr --output eDP-1-1 --mode 2048x1536
until steam steam://run/730 | grep "Game removed" ; do
        :
done
echo "Success!"
xrandr --output eDP-1-1 --mode 3840x2160

All my script does is to change resolution, start a Steam game, and then wait for a message to appear in the output which indicates the game was closed and change the resolution back to what it was.

The message I've mentioned appears every time, but the Steam process is still running. Could that be the problem?

If I close Steam manually then the script continues like I want it to.

Upvotes: 1

Views: 833

Answers (1)

Inian
Inian

Reputation: 85895

You need to use the until built-in construct for this,

until command-name | grep -q "Success!"; do
   :
done

This will wait indefinitely until the string you want appears in stdout of your command, at which point grep succeeds and exits the loop. The -q flag in grep instructs it to run it silent and : is a short-hand for no-operation.


Difference between until and while - the former runs the loop contents until the exit-code of the command/operator used becomes 0 i.e. success, at which point it exits and while does the vice-versa of that.

Upvotes: 4

Related Questions