![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#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 |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
Quote:
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
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
|
|
#3
|
||||
|
||||
|
sb008 you've replicated one error...spaces around the "="
|
|
#4
|
|||
|
|||
|
Quote:
|
|
#5
|
||||
|
||||
|
Alternative syntax...
Code:
if ! grep -qi lisa daily.log then echo not found fi |
|
#6
|
|||
|
|||
|
Quote:
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
+ 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! |
|
#7
|
|||
|
|||
|
Does anyone have any ideas as to what I could try next, or what this could possibly be?
|
|||
| Google The UNIX and Linux Forums |
| Thread Tools | |
| Display Modes | |
|
|