Using GREP in IF Statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using GREP in IF Statement
# 1  
Old 05-15-2013
Using GREP in IF Statement

Hello All,
I have 2 different pieces of code, I am confused why the Code1 is giving me the correct result where as the Code2 is not giving me correct result. It gives me always result as "Failure" irrespective of the "ERROR" word exists in logfile or not. may I know the reason why? I am using Bash shell in Linux OS.

Code1:
Code:
#!/bin/bash
sqlplus -s FCSDWH_STG/FCSDWH_STG@dwdev <<EOF > /home/infrmtca/bin/logfile
exec today_is;
EOF
        if `grep -q "ERROR" /home/infrmtca/bin/logfile` ;
        then
                echo "Success"
        else
                echo "Failure"
        fi

Code2:
Code:
#!/bin/bash
sqlplus -s FCSDWH_STG/FCSDWH_STG@dwdev <<EOF > /home/infrmtca/bin/logfile
exec today_is;
EOF
        if [ `grep -q "ERROR" /home/infrmtca/bin/logfile` ]
        then
                echo "Success"
        else
                echo "Failure"
        fi

My content of "/home/infrmtca/bin/logfile" is below
Code:
BEGIN today_is; END;

      *
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00905: object FCSDWH_STG.TODAY_IS is invalid
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

Thank you
# 2  
Old 05-15-2013
I assume you want to print message: Failure when you find the pattern: ERROR in your file.

Few corrections and modifications:
Code:
if grep -q "ERROR" /home/infrmtca/bin/logfile 
then
      echo "Failure"
else
      echo "Success"
fi

# 3  
Old 05-15-2013
Quote:
Originally Posted by Yoda
I assume you want to print message: Failure when you find the pattern: ERROR in your file.

Few corrections and modifications:
Code:
if grep -q "ERROR" /home/infrmtca/bin/logfile 
then
      echo "Failure"
else
      echo "Success"
fi

But why is it not working when I put the same in [] like [if grep -q "ERROR" /home/infrmtca/bin/logfile]

Error:
Code:
/home/infrmtca/bin/test.sh: line 5: [: too many arguments
Failure

# 4  
Old 05-15-2013
Quote:
Originally Posted by Ariean
But why is it not working when I put the same in [] like [if grep -q "ERROR" /home/infrmtca/bin/logfile]
Please note that square brackets [] that contain the statement are a synonym for the 'test' command.

So it is expecting a valid expression and it is failing because the command that you put inside doesn't meet the criteria.

This is why you don't need a square bracket [] to wrap the command.

The grep command returns the exit status 0 or non-zero which if statement can check and transfer the control to corresponding action.
# 5  
Old 05-23-2013
(1)
Quote:
Originally Posted by Yoda
Please note that square brackets [] that contain the statement are a synonym for the 'test' command.

So it is expecting a valid expression and it is failing because the command that you put inside doesn't meet the criteria.

This is why you don't need a square bracket [] to wrap the command.

The grep command returns the exit status 0 or non-zero which if statement can check and transfer the control to corresponding action.
Isn't greping for a string "ERROR" is also a test? what do you mean by test? you mean comparing 2 variables or values using -eq for numbers and == for strings or a -z or -n for zero & nonzero byte strings, something like that...Can you please explain a bit more with example

(2) I am trying the below script to check 2 conditions

Code:
#!/bin/bash
PATH="/db_apps/informatica/infa951/server/infa_shared/BWParam/wf_GenerateXMLDataFile.txt"
UNINUM="92500"
echo $UNINUM

        if  /bin/grep -q "UNINUM" $PATH -a -z ${UNINUM}
        then
                echo "success"
        else
                echo "failure"
        fi

But it always returns success, i am confused.

Thank you.

Last edited by Ariean; 05-23-2013 at 03:33 PM..
# 6  
Old 05-23-2013
Code:
if [ 4 = 4 ]
then
  echo "true"
fi

is the same as
Code:
if test 4 = 4
then
  echo "true"
fi

grep, like test, sets an exit status that "if" can use.
But grep is a different command.

Indeed [ ] is not an operator; the [ is the test command plus it wants a ] as last argument.
Code:
type test
test is a shell builtin
type \[
[ is a shell builtin


Last edited by MadeInGermany; 05-23-2013 at 03:46 PM..
# 7  
Old 05-23-2013
Is there a way for me to combine a grep command and an expression statement and pass the result to IF statement like below. I am still trying to make the below script work.

Code:
#!/bin/bash
PATH="/db_apps/informatica/infa951/server/infa_shared/BWParam/wf_GenerateXMLDataFile.txt"
UNINUM="92500"
echo $UNINUM

        if  /bin/grep -q "UNINUM" $PATH -a -z ${UNINUM}
      #if  [ `/bin/grep -q "UNINUM" $PATH` -a -z ${UNINUM} ]
        then
                echo "success"
        else
                echo "failure"
        fi

---------- Post updated at 03:36 PM ---------- Previous update was at 03:32 PM ----------

Quote:
Originally Posted by Ariean
Is there a way for me to combine a grep command and an expression statement and pass the result to IF statement like below. I am still trying to make the below script work.

Code:
#!/bin/bash
PATH="/db_apps/informatica/infa951/server/infa_shared/BWParam/wf_GenerateXMLDataFile.txt"
UNINUM="92500"
echo $UNINUM

        if  /bin/grep -q "UNINUM" $PATH -a -z ${UNINUM}
      #if  [ `/bin/grep -q "UNINUM" $PATH` -a -z ${UNINUM} ]
        then
                echo "success"
        else
                echo "failure"
        fi

I think i got it

Code:
#!/bin/bash
PATH="/db_apps/informatica/infa951/server/infa_shared/BWParam/wf_GenerateXMLDataFile.txt"
UNINUM="92500"
echo $UNINUM

        if  ! `/bin/grep -q "UNINUM" $PATH` && [ ! -z ${UNINUM} ]
        then
                echo "success"
        else
                echo "failure"
        fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Ps -ef|grep <processname> usage in IF statement

Hi, Am working in a filenet domain where we are using AIX as our terminal to run the jobs and schedule the shell scripts to run . In my previous post regarding the "Log modification with finding errors" ... (12 Replies)
Discussion started by: Kalaihari
12 Replies

2. UNIX for Dummies Questions & Answers

Grep Segmentation Fault If statement

I receive a segmentation fault on some servers when grepping a string, but not when I use -i, and vice-versa. I have googled this and realise it is usually a hardware / memory issue. I'd like to write an if statement that says "if grep string returns 'Segmentation fault' then grep the same... (4 Replies)
Discussion started by: MaindotC
4 Replies

3. Shell Programming and Scripting

Use awk/sed/grep with goto statement!

Hi, I have an array with characters and I am looking for specific character in that array and if those specific character not found than I use goto statment which is define somehwhere in the script. My code is: set a = (A B C D E F) @ i = 0 while ($i <= ${#a}) if ($a != "F" || $a != "D")... (3 Replies)
Discussion started by: dixits
3 Replies

4. Shell Programming and Scripting

if statement with grep as conditional

Please see the script segment below for i in $files do echo $i if ; then case "$1" in "IE0263"|"IE0264"|"IE0267"|"IE0268") short_filename=`ls -l $i | cut -c108-136 | sort` ;; "IE0272"|"IE0273") short_filename=`ls -l $i | cut... (4 Replies)
Discussion started by: jmahal
4 Replies

5. Shell Programming and Scripting

sed / grep / for statement performance - please help

I'm searching the most effective way of doing the following task, so if someone can either provide a working solution with sed or one totally different but more effective then what I've got so far then please go ahead! The debugme directory has 3 subdirectorys and each of them has one .txt file... (7 Replies)
Discussion started by: TehOne
7 Replies

6. Shell Programming and Scripting

Grep statement

Hi All, Please can somebody advise that if I want to search a pattern xyz the grep command should only select xyz and not any other pattern containing xyz (ex abxyzcd) Regards (1 Reply)
Discussion started by: Shazin
1 Replies

7. UNIX for Dummies Questions & Answers

grep entire statement not just line

(extract from SQL binlog file...) # at 4960 #080801 14:35:31 server id 4 end_log_pos 195 Query thread_id=63121426 exec_time=0 error_code=0 use d_jds; SET TIMESTAMP=1217581531; UPDATE bid_details set bidding = 3170.37 ,deduction=if((3170.37 < 37.43),0,deduction) where... (3 Replies)
Discussion started by: shantanuo
3 Replies

8. Shell Programming and Scripting

Swich statement based on a grep?

I'm piping the results of a tcpdump into a shell script and want certain commands executed (specifically the firing of an SNMP alert) based on a grep for certain contents in the output of tcpdump. For example, I have #!/bin/bash while read str; do grep 'ttl 64' -q && sudo snmptrap -v 1... (2 Replies)
Discussion started by: paulobrad
2 Replies

9. Shell Programming and Scripting

Using grep in a test/if statement

Okay, well this is more or less my first attempt at writing a shell script. Anyways, here's my code: cd ${PATH} if then rm ${FILE} ./anotherScript else exit 1 fi exit 1 Anyways, it's a pretty simple script that is supposed to search for the... (4 Replies)
Discussion started by: cbo0485
4 Replies

10. Shell Programming and Scripting

Using grep in if statement

Can somebody please guide me towards right syntax: #!/bin/ksh if i = $(grep $NAME filename) echo "Name Found" else echo " Name not Found" fi I need to grep for $NAME in the file, and if it returns false, execute a series of commands and if true, exit out. The above is not the right... (3 Replies)
Discussion started by: chiru_h
3 Replies
Login or Register to Ask a Question