Artik
Artik

Reputation: 85

Relaunch program when grep returns a result

I'm trying to write a little bash script that launch a wine .exe file (Photoshop). During the launch, if there is a boot error, the message "Assertion failed" is displayed.

The concept would be:

  1. I launch Photoshop using wine

wine64 "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe"

  1. I grep the output, to see if a boot error occurs:

2>&1 | grep -i Assertion

  1. If there is an error, I stop the execution of wine using pipefail, and try to relaunch photoshop Looping this till it boots.

I tried to write a little script that is obviously wrong. How to make it works?

#!/bin/sh

set -euxo pipefail

wine64 "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe" 2>&1 | grep -i -L Assertion

if [ Assertion failed ]
then
        wine64 "/home/artik/.wine/drive_c/Program Files/Adobe/Adobe Photoshop CC 2019/Photoshop.exe" 2>&1 | grep -i -L Assertion
fi

Upvotes: 0

Views: 58

Answers (1)

that other guy
that other guy

Reputation: 123610

One way would be:

while grep -q Assertion < <(wine64 ... 2>&1)
do
  pkill wine64
done

grep -q will immediately exit with success when Assertion is found, and since the input is from a process substitution, it will then not wait for wine64 to exit. This causes the loop to be entered and Wine to be restarted.

If the program exits without grep finding Assertion, then grep exits with failure causing the loop to stop.

Upvotes: 1

Related Questions