Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using awk command in an if...then statement, or suggest another way. 1

Status
Not open for further replies.

Elsebet

Programmer
Aug 7, 2003
38
US

Hi all!

While not altogether new to ksh scripting I'm certainly no expert. What I am trying to do currently is read Oracle log files and determine if any errors occured. If so, I'd like to get the log file automatically mailed to me. The script is mostly working, except for the part where I use AWK to find if the value in the "Number of rows rejected" is greater than zero. It works fine as a single command both in and out of the script, I just can't get it to work in a conditional statement or assign the value to a variable. I have searched this forum, used google, read the faqs, but still am not having any luck. Here is the script, with my variety of attempts:

Code:
#
# This script will parse an Oracle log file and if any ERROR or ORA- lines
# are found, will e-mail the log name, location, and error lines.
#
# Parms:
# $1 = log file name

logdir=$HOME/bin/load/logs/
GREP=/usr/bin/grep
AWK=/usr/bin/awk
recipients=test@test.com

rejected=$AWK '/rejected/ {print $5}' $logdir$1

#if $GREP ORA- $logdir$1; then
#   cat $logdir$1 | mailx -s "Error in $1" $recipients;
#fi

#if (( $AWK \'/rejected/ {print $5}\' $logdir$1 -gt 0 )); then
#   cat $logdir$1 | mailx -s "Error in $1" $recipients;
#fi   

#$AWK '/rejected/ {print $5}' $logdir$1

#if [ $AWK '/rejected/ {print $5}' $logdir$1 != 0 ]; then
#   cat $logdir$1 | mailx -s "Error in $1" $recipients;
#fi

if [ $rejected != 0 ]; then
   cat $logdir$1 | mailx -s "Error in $1" $recipients;
fi

Any ideas would be greatly appreciated! In the meantime I'll keep working on it.
 
try this
Code:
if (( $($AWK '/rejected/ {print $5}' $logdir$1) > 0 )); then
   cat $logdir$1 | mailx -s "Error in $1" $recipients;
fi

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
vgersh99 said:

That worked perfectly! Thank you, much appreciated. I understand the (( )) on the outside signify an integer comparison (right?) but I'm not sure what the $() around the command signifies, since the grep statement was fine without quotes, but then again it was a boolean...hrm.

Just more for me to learn! Thanks again!

 
your 'if' has 3 parts: value1 oper value2

Your first value is the return value of 'awk' - you need to make the AWK statement an Command Substitution - hence $(). You could also use 'back-ticks' to make it active.

'man ksh' and search for 'Command Substitution'

The returned value is numerically compared to 'value2' - '0' in your case.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Another way:
rejected=$($AWK '/rejected/ {print $5}' $logdir$1)
if [ $rejected != 0 ]; then
cat $logdir$1 | mailx -s "Error in $1" $recipients;
fi

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top