Grep -q -F , what is the function of this script

Hello,
I checked grep help field, I got the answer but seems a bit technical for me.
Could you please let me know what is this script doing?

grep -q -F 'addprestart.sh' /usr/bin/enigma2_pre_start.sh || \
echo '/bin/sh /etc/enigma2/addprestart.sh > \
/dev/null 2>&1 & sleep .5 &' >>
/usr/bin/enigma2_pre_start.sh

This field is ok:

echo '/bin/sh /etc/enigma2/addprestart.sh > \
/dev/null 2>&1 & sleep .5 &' >>
/usr/bin/enigma2_pre_start.sh

It prints
/bin/sh ...... sleep .5 &
into enigma2_pre_start.sh file

But what is this command doing?

grep -q -F 'addprestart.sh' /usr/bin/enigma2_pre_start.sh 

I'd appreciate if you could shed light on this .

Many thanks
Boris

The -q means to be quiet or do not output anything to stdout but only exit with a zero (success) as soon as one match is found.
The -F means to interpret the search pattern as a string and not as a regular expression. In this case find the literal string " addprestart.sh " in the file /usr/bin/enigma2_pre_start.sh

I do not know if you missed it but there's an OR logic operator in the command which means if that grep exit with other than zero the echo is executed.

In more human words, append the line /bin/sh /etc/enigma2/addprestart.sh to the script /usr/bin/enigma2_pre_start.sh if it doesn't have it already.

1 Like

Dear Aia,
Thank you for your last sentence. By the way, I did not understand the function of quiet .
if we do not use quiet , does it print out the grabbed data?
Finally, "double pipe" is the or logic operator, is that true?

Many thanks
Boris

Hello, Boris.

If "quiet" ( -q ) isn't used then grep will print any text it finds, to standard output (you can, similarly, make it "quiet" using grep ... >/dev/null). Using -q does not affect the return code of the command. That's what's captured with || . Using || is telling the shell to execute whatever follows, if grep fails (returns a non-zero return code). If you want to execute the "/bin/sh..." on a successful grep match, you should use && instead. Sometimes with these multi-line type of commands, it's simpler just to use an if statement.

1 Like

Dear Scott,
Thanks for your answer.
Aia mentioned or logic operator. I suppose what he implied is about below example:

$ false && echo howdy!

$ true && echo howdy!
howdy!
$ true || echo howdy!

$ false || echo howdy!
howdy!

I got the point clearly now.

Kind regards
Boris

grep -q is more than just suppressing any output, it will exist with success as soon as it finds a match, avoiding to continue searching any other lines in the file until it reaches the end of the file. Therefore
grep ... > /dev/null will not be the same

3 Likes

Very good point. Thank you.