The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grep within a script dnash Shell Programming and Scripting 13 05-16-2008 02:26 AM
grep script drchris UNIX for Dummies Questions & Answers 4 05-17-2007 06:32 AM
Unix shell script (grep -A 6 -B 2 "ORA-" filename el_guero Shell Programming and Scripting 7 04-25-2007 02:37 PM
grep from a script... hamsasal UNIX for Advanced & Expert Users 1 08-14-2005 01:24 AM
script ksh/awk/grep hoang Shell Programming and Scripting 2 06-01-2002 12:20 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 03-12-2007
Registered User
 

Join Date: Mar 2007
Posts: 1
Using Grep within a UNIX Script

Hi,

I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated!

--------------------

set xTest = 'grep -i lisa daily.log'

if [$xTest == 0] ;
then
echo "TEST" >> daily.log
fi
--------------------------


Thanks,
Lisa
Reply With Quote
Forum Sponsor
  #2  
Old 03-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by lisa_0801
Hi,

I'm trying to save the wc from a grep command in my unix script and then later in my script I try to reference the variable but I have no luck. Any help is greatly appreciated!

--------------------

set xTest = 'grep -i lisa daily.log'

if [$xTest == 0] ;
then
echo "TEST" >> daily.log
fi
--------------------------


Thanks,
Lisa
"grep i lisa daily.log" does not return the a count. It returns all ines in the file "daily.log" which contain the string "lisa" (case insensative).

If you are only interested if the string "lisa" does occur in the file you can use this command.

However you will have to check on the return code. If 0 the string "lisa" was found, if "1" the string was not found.

Furthermore, the "==" in the if condition is to compare strings, and not to check if a variable has a specific integer value. To check for integer values you use "-eq", "-ne", "-gt", "-ge", "-lt" or "-le".

Just to check if the srting occurs in the file you can use the following script.

Code:
#!/usr/bin/ksh

xTest = 'grep -i lisa daily.log'

if [ ${?} -ne 0 ] 
then
  echo "TEST" >> daily.log
fi
"${?}" is a reference to the return code of the command "grep".

Since you want to add the line "TEST" to the file if the string "lisa" did not occur in the file, it means the return code of grep has to be NON 0 (grep didn't find the string).

If you want to work with the number of times the string occurs you can use the following script.

Code:
#!/usr/bin/ksh

typeset -i xTest
xTest = 'grep -i lisa daily.log | wc -l'

if [ ${xTest} -eq 0 ] 
then
  echo "TEST" >> daily.log
fi
By adding the "| wc -l" the number of occurences is counted.
Reply With Quote
  #3  
Old 03-12-2007
reborg's Avatar
Administrator
 

Join Date: Mar 2005
Location: Ireland
Posts: 3,590
sb008 you've replicated one error...spaces around the "="
Reply With Quote
  #4  
Old 03-12-2007
Registered User
 

Join Date: Jan 2007
Posts: 366
Quote:
Originally Posted by reborg
sb008 you've replicated one error...spaces around the "="
You are correct
Reply With Quote
  #5  
Old 03-12-2007
Ygor's Avatar
Moderator
 

Join Date: Oct 2003
Location: -31.96,115.84
Posts: 1,248
Alternative syntax...
Code:
if ! grep -qi lisa daily.log
then
   echo not found
fi
Reply With Quote
  #6  
Old 04-18-2008
Registered User
 

Join Date: Apr 2008
Posts: 5
Question

Quote:
Originally Posted by sb008 View Post
Since you want to add the line "TEST" to the file if the string "lisa" did not occur in the file, it means the return code of grep has to be NON 0 (grep didn't find the string).

If you want to work with the number of times the string occurs you can use the following script.

Code:
#!/usr/bin/ksh

typeset -i xTest
xTest = 'grep -i lisa daily.log | wc -l'

if [ ${xTest} -eq 0 ] 
then
  echo "TEST" >> daily.log
fi
By adding the "| wc -l" the number of occurences is counted.
Hi,

I am doing something very similar to this, only with two scripts - one main script and this little function in another script. The main script calls this grep function multiple times, looking for the word "Error". It gets as far as the grep statement, and then exits the whole program. X is the variable I used to store the result of the grep.

The grep script:
Code:
ls -lt $DEFDIR/outfile
if [ -s $DEFDIR/outfile ]
then
   x=`grep -ci 'Error' $DEFDIR/outfile`
   if [ ${x} -eq 0 ]
   then                       
            echo 'Process successful'
            echo "for Input data $INPUT_FILE"
   else                                             
            echo 'Error in file $INPUT_FILE' 
   fi
fi
So, my standard list for my main script ends up looking like:

+ cd <directory the scripts are in>
+ <execute grep script call>
+ <list the file outfile>
+ [ -s outfile ]
+ + grep -ci Error outfile
x=0
+ echo logout
logout

Script completed with exit code 1

The grep should echo "Process completed successfully" if grep didn't find "Error", but instead it just exits the whole script. I should note that this script used to execute fine until I added the shell specification code #!/bin/ksh -e. I know that the -e forces the script to exit if it finds an error, but if the grep statement passes I do not see where the error could be.

Thanks ahead of time for all your help!
Reply With Quote
  #7  
Old 04-21-2008
Registered User
 

Join Date: Apr 2008
Posts: 5
Does anyone have any ideas as to what I could try next, or what this could possibly be?
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 12:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0