INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

awk file in if statement then statement KSH

awk file in if statement then statement KSH

(OP)
if [[ awk '{ print $1 =< 100 }' PERCENT_ALERT.txt ]] ; then

/usr/lib/sendmail -v -f "ALERT" "email@address.com" < ALERT.txt

else

echo "File is Empty"

#echo "File is Empty" | mailx -s "FILE_EMPTY" herb.glaser@uscellular.com

fi;

RE: awk file in if statement then statement KSH

Hi

Between the [[ and ]] an expression is expected. And expression != command.

To make your command being executed, enclose it in `...` or $(...).

But probably this will not work as you expected. Better explain your goal and show some sample data from PERCENT_ALERT.txt.

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
Thanks for the help.

The PERCENT_ALERT.txt is a single line file that only has a numeric value as such.

98

RE: awk file in if statement then statement KSH

(OP)
Better explanation is if the value in the PERCENT_ALERT.txt file is less than or equal to 100 then send an email with the text of a different file.

Hope that makes sense.

RE: awk file in if statement then statement KSH

Hi

If you are sure the file will always contain one integer, then I would prefer this way :

CODE --> (ba|k)sh

if (( $(< PERCENT_ALERT.txt ) <= 100 )); then
/usr/lib/sendmail -v -f "ALERT" "email@address.com" < ALERT.txt
else
echo "File is Empty"
fi
Tested with bash and mksh.

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
It seems to be working as far as the if portion, however it this is in a KSH script.

When it meets the criteria of the if statement I get several emails in a row with nothing in them other then ALERT as the subject.

if (( $(< PERCENT_ALERT.txt ) <= 100 )); then

/usr/lib/sendmail -v -f "ALERT" "email@address.com" < ALERT.txt

else

echo "File is Empty"

fi;

Not sure why it repeats itself over and over ??? And not including the ALERT.txt information.

RE: awk file in if statement then statement KSH

Hi

Interesting. What happens when you move the reading of PERCENT_ALERT.txt out from the if condition ?

CODE

percent=$(< PERCENT_ALERT.txt )

if (( percent <= 100 )); then
/usr/lib/sendmail -v -f "ALERT" "email@address.com" < ALERT.txt
else
echo "File is Empty"
fi

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
Still get about 9 emails with only a subject.

RE: awk file in if statement then statement KSH

Hi

Let us add some debugging. Please post the output produced by this script :

CODE

percent=$(< PERCENT_ALERT.txt )
echo "percent='$percent'"

if (( percent <= 100 )); then
echo 'percent is small'
else
echo 'percent is big'
fi

if (( percent <= 100 )); then
echo 'before send'
/usr/lib/sendmail -v -f "ALERT" "email@address.com" < ALERT.txt
echo 'after send'
else
echo "File is Empty"
fi

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
percent='99'
percent is small
before send
email@address.com... Connecting to corpmta.com. via relay...
220 wimidd01-smtp-01-internal.com ESMTP
>>> EHLO ilscha01-nsweng-01.com
250-wimidd01-smtp-01-internal.com
250-8BITMIME
250 SIZE 16777216
>>> MAIL From:<ALERT@ilscha01-nsweng-01.com> SIZE=36
250 sender <ALERT@ilscha01-nsweng-01.com> ok
>>> RCPT To:<email@address.com>
250 recipient <email@address.com> ok
>>> DATA
354 go ahead
>>> .
250 ok: Message 408188946 accepted
email@address.com... Sent (ok: Message 408188946 accepted)
Closing connection to corpmta.com.
>>> QUIT
221 wimidd01-smtp-01-internal.com
after send

RE: awk file in if statement then statement KSH

Hi

Looks correct for me.

And if you run only the sendmail command without the rest of the script, still get multiple mails ?

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
/usr/lib/sendmail -v -f "ALERT" email@address.com < ALERT.txt

I only get 1 email when I do this, however I only see ALERT in the sender information. No information from the ALERT.txt file is included.

I was including the if statement at the end of a cron that runs every five minutes. However every five minutes I was getting 9 emails.

Crazy.

RE: awk file in if statement then statement KSH

Hi

Could you show us more from that script ? Maybe the relevant crontab line(s) too.

Feherke.
http://feherke.github.com/

RE: awk file in if statement then statement KSH

(OP)
I have a get_script that kicks off the other script. The reason is I'm pulling the same data from several places at once ever 5 minutes.

CRON Entry
1,6,11,16,21,26,31,36,41,46,51,56 * * * * /export/home/scripts/get_mtxcper

cat get_mtxcper
/export/home/scripts/mtxcper STLOMTX &

cat mtxcper
#!/usr/bin/ksh
# echo "Enter switch: \c"
# read switch
# echo "Enter username: \c"
# read username
# echo "Enter password: \c"
# read password
switch=$1
dt=$(date +%m%d%y_%H%M)
username=username
password=password

for x in 1
do
{
sleep 5
echo $username
sleep 3
echo $password
sleep 3
echo "get cper"
sleep 3
echo "exit;"

}|telnet $switch>/export/home/live/cperlive/$switch.txt

done

tail +45 /export/home/live/cperlive/STLOMTX.txt > /export/home/live/cperlive/STLOMTX.LIVE.txt

cat /export/home/live/cperlive/STLOMTX.txt | grep "BHCA" > /export/home/live/cperlive/BHCA.txt

cat /export/home/live/cperlive/STLOMTX.txt | grep "Origination" > /export/home/live/cperlive/Origination.txt

awk '{ print $4, $NF }' < /export/home/live/cperlive/BHCA.txt > /export/home/live/cperlive/CPER_ALERT.txt

awk '{ print $2, $3 }' < /export/home/live/cperlive/Origination.txt >> /export/home/live/cperlive/CPER_ALERT.txt

cat /export/home/live/cperlive/Origination.txt | grep "Voice" > /export/home/live/cperlive/voice.txt

awk '{print $3 }' < /export/home/live/cperlive/voice.txt > /export/home/live/cperlive/percent.txt

sed 's/%//' /export/home/live/cperlive/percent.txt > /export/home/live/cperlive/PERCENT_ALERT.txt

percent=$(< /export/home/live/cperlive/PERCENT_ALERT.txt )

echo "percent='$percent'"

if (( ( percent ) <= 100 )); then

/usr/lib/sendmail -v -f "CPER_ALERT" "email@address.com" < /export/home/live/cperlive/CPER_ALERT.txt

else

echo "File Not Found"

fi;

RE: awk file in if statement then statement KSH

(OP)
I figured out the issue. Apparently the file I wanted to be included in the email can't have () or : in it. Once I removed those the script works fine.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close