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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

awk file in if statement then statement KSH

Status
Not open for further replies.

hg4372

Technical User
Feb 9, 2009
43
0
0
US
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;
 
Hi

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

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

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

Feherke.
[link feherke.github.com/][/url]
 
Thanks for the help.

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

98

 
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.
 
Hi

If you are sure the file will always contain one integer, then I would prefer this way :
Code:
[b]if[/b] [teal](([/teal] [navy]$([/navy][teal]<[/teal] PERCENT_ALERT[teal].[/teal]txt [teal])[/teal] [teal]<=[/teal] [purple]100[/purple] [teal]));[/teal] [b]then[/b]
  /usr/lib/sendmail -v -f [green][i]"ALERT"[/i][/green] [green][i]"email@address.com"[/i][/green] [teal]<[/teal] ALERT[teal].[/teal]txt
[b]else[/b]
  echo [green][i]"File is Empty"[/i][/green]
[b]fi[/b]
Tested with [tt]bash[/tt] and [tt]mksh[/tt].

Feherke.
[link feherke.github.com/][/url]
 
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.
 
Hi

Interesting. What happens when you move the reading of PERCENT_ALERT.txt out from the [tt]if[/tt] condition ?
Code:
[navy]percent[/navy][teal]=[/teal][navy]$([/navy][teal]<[/teal] PERCENT_ALERT[teal].[/teal]txt [navy])[/navy]

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

Feherke.
[link feherke.github.com/][/url]
 
Still get about 9 emails with only a subject.
 
Hi

Let us add some debugging. Please post the output produced by this script :
Code:
[navy]percent[/navy][teal]=[/teal][navy]$([/navy][teal]<[/teal] PERCENT_ALERT[teal].[/teal]txt [navy])[/navy]
[highlight]echo [green][i]"percent='$percent'"[/i][/green][/highlight]

[highlight][b]if[/b] [teal](([/teal] percent [teal]<=[/teal] [purple]100[/purple] [teal]));[/teal] [b]then[/b][/highlight]
  [highlight]echo [green][i]'percent is small'[/i][/green][/highlight]
[highlight][b]else[/b][/highlight]
  [highlight]echo [green][i]'percent is big'[/i][/green][/highlight]
[highlight][b]fi[/b][/highlight]

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


Feherke.
[link feherke.github.com/][/url]
 
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
 
Hi

Looks correct for me.

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

Feherke.
[link feherke.github.com/][/url]
 
/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.
 
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;

 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top