Sponsored Content
Top Forums Shell Programming and Scripting "if" statement based off "grep" Post 302652525 by bakunin on Thursday 7th of June 2012 08:30:54 AM
Old 06-07-2012
@Amzerik:

You might think that you have asked a very specific question and all you wanted is a specific answer. Now my colleagues are questioning your motives. Please, let me explain.

We oftenly have the case that question "how can i achieve X" is asked because what is really needed is Y but the thread opener is under the (false) impression that X is necessary to achieve Y. It may be that in fact X is a suboptimal way to achieve Y and as soon as we recognize that we try to help the thread opener achieve his goal instead of answering his question. Therefore all the questions about your OS, the version, your true goal, etc.

Regarding your initial question: "if" takes a list of commands as an argument and branches depending on its exit code. The following will work:

Code:
if true ; then
    echo "tested to TRUE"
else
     echo "tested to FALSE"
fi

(Of course, the else-part will never be executed because "true" always returns 0, whereas "false" always returns 1. Replace "true" with "false" to see this effect. "true" and "false" are both commands built for exactly this purpose.)

In shell programming, logical TRUE and FALSE are handled like in C: 0 is TRUE, everything else is FALSE. Now "grep -q" returns "0" when it finds the search pattern in its input and "1" if not. Therefore we could write:

Code:
if grep -q "foo" /path/to/file/containing-foo ; then
    ...

and the if-branch would be executed if the file contains "foo" while the else-part will be executed of the file doesn't contain "foo".

Now, how does that fit in with what we usually see as if-statements?

Originally, there was a command "test": it was fed an expression which evaluated to TRUE or FALSE and returned an exit status depending on this. See the man page for "test" for details. The following should be obvious to you now ("-ge" means "greater or equal"):

Code:
if test 1 -ge 0 ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

(By the way, this is why it is a really BAD idea to name a program "test". It will collide with the original "test" from the system, which is still there.)

Now, to let shell code look more like traditional programming languages someone came up with the clever idea to make "[" a link to "test". "test" could now be invoked by "[" too and the following could be written:

Code:
if [ 1 -ge 0 ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

but that still looked counter-intuitive, because the opening "[" missed a counterpart. Therefore "test" itself was modified to have "]" as its (last) argument (well, actually only its "["-variant was changed that way). Now the necessary line looked like what we are used to:

Code:
if [ 1 -ge 0 ] ; then
     echo "yes, it worked"
else
     echo "something went wrong"
fi

Notice, this is the reason, why "[" has to be surrounded by spaces. if [ $int1 -ge $int2 ]; will work, whereas if [$int1 -ge $int2]; won't. It is for the same reason that ls -l works but ls-l doesn't.

I hope this helps.

bakunin

Last edited by bakunin; 06-07-2012 at 09:38 AM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies

2. Shell Programming and Scripting

"Join" or "Merge" more than 2 files into single output based on common key (column)

Hi All, I have working (Perl) code to combine 2 input files into a single output file using the join function that works to a point, but has the following limitations: 1. I am restrained to 2 input files only. 2. Only the "matched" fields are written out to the "matched" output file and... (1 Reply)
Discussion started by: Katabatic
1 Replies

3. Shell Programming and Scripting

ps -ef | grep "string1" "string2" " "string3"

Hi all, can any one suggest me the script to grep multiple strings from ps -ef pls correct the below script . its not working/ i want to print OK if all the below process are running in my solaris system. else i want to print NOT OK. bash-3.00$ ps -ef | grep blu lscpusr 48 42 ... (11 Replies)
Discussion started by: steve2216
11 Replies

4. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

5. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

6. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

7. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

8. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

9. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies
All times are GMT -4. The time now is 07:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy