Visit Our UNIX and Linux User Community


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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grep -q -F , what is the function of this script
# 1  
Old 09-06-2018
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?


Code:
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:
Code:
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?
Code:
grep -q -F 'addprestart.sh' /usr/bin/enigma2_pre_start.sh


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

Many thanks
Boris

Last edited by baris35; 09-07-2018 at 01:41 PM..
# 2  
Old 09-06-2018
Quote:
Originally Posted by baris35
[...]
Code:
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

[...]
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.

Last edited by Aia; 09-06-2018 at 09:42 PM.. Reason: Human
This User Gave Thanks to Aia For This Post:
# 3  
Old 09-07-2018
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
# 4  
Old 09-07-2018
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.
This User Gave Thanks to Scott For This Post:
# 5  
Old 09-07-2018
Dear Scott,
Thanks for your answer.
Aia mentioned or logic operator. I suppose what he implied is about below example:

Code:
$ false && echo howdy!

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

$ false || echo howdy!
howdy!

I got the point clearly now.

Kind regards
Boris
# 6  
Old 09-07-2018
Quote:
Originally Posted by Scott
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 [b]grep ... >/dev/null[/b]). 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.
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
These 3 Users Gave Thanks to Aia For This Post:
# 7  
Old 09-07-2018
Quote:
Originally Posted by Aia
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
Very good point. Thank you.

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Will files, creaetd in one function of the same script will be recognized in another function?

Dear All. I have a script, which process files one by one. In the script I have two functions. one sftp files to different server the other from existing file create file with different name. My question is: Will sftp function recognize files names , which are created in another... (1 Reply)
Discussion started by: digioleg54
1 Replies

2. Shell Programming and Scripting

What is the function of the following lines at the top of a shell script file: Directory and Script?

The file starts like this: Directory: <path to the script> Script: <script fife name> #!bin/ksh ##Comments <actual script> What is the use of the first two lines in the script? What if I save the file without them? What will be the effect? They are not comments. Im very new to this,... (4 Replies)
Discussion started by: remytom
4 Replies

3. UNIX for Dummies Questions & Answers

GREP function in ksh which ignores LINE Breaks

Hello I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from... (24 Replies)
Discussion started by: Raghav Garg
24 Replies

4. How to Post in the The UNIX and Linux Forums

GREP function in ksh which ignores LINE Breaks

I am using a grep command with two patterns in my KSH script. File has line breaks in it and both the patterns are in different lines. Here is the command - grep -l 'RITE AID.*ST.820' natriter820u.20140914 Pattern1 - RITE AID Pattern2 - ST*820 I am not getting any results from this,... (3 Replies)
Discussion started by: Raghav Garg
3 Replies

5. Shell Programming and Scripting

Help with grep and read function in a BASH Script

I'm putting together a script that will search my mail archives for emails that meet certain criteria and output the files to a text file. I can manually cat that text file and pipe it into sendmail and it will work (i.e. cat /pathtofile/foo.txt | sendmail -t me@company.com) My script sends... (7 Replies)
Discussion started by: binary-ninja
7 Replies

6. Shell Programming and Scripting

Cannot get grep to work within function.

Hello again, Am having an issue now with getting a simple grep command to work within a function.. The function is as below... function findRecord() { output=grep "001" recordDatabase echo $output } At the moment the "001"... (3 Replies)
Discussion started by: U_C_Dispatj
3 Replies

7. UNIX Desktop Questions & Answers

grep function

Hi Guys, I have a very limited knowledge on shell scripting. When I execute dspmq, I get either " Running" or "Running in Standby" as output $dspmq QM1 Running QM2 Running as StandByI want my script to run only if the output of dspmq is "Running". I executed the below... (6 Replies)
Discussion started by: vandi
6 Replies

8. Shell Programming and Scripting

Perl - Grep function regular expression

For some reason, @logs is a list of log files @filter is a list of expressions to grep out foreach (@logs){ open READ, "<$_" or die $!; @temp=<READ>; close READ; foreach (@filter){ print grep /$_/,@temp ; } } returns a regex error in one of the files... (4 Replies)
Discussion started by: adelsin
4 Replies

9. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

10. Shell Programming and Scripting

Help in grep function or similar using awk

I have a list of id; for example: file 1 dfghd dfghe dfgey dfgeu I have another data file that contain this ids as headers; for ex. file2 >dfghd gfdgfddl;klfkld;ld;lgl;dld'l'dv >dfghe gkwhjhsgdjdjdjhjddj >dfgey jdkjfhdjhfdkjhfdkhkdk I wanted to compare file 1 and file 2... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

Featured Tech Videos