Re: Run awk script from within KSH script (533 Views)
Reply
Occasional Contributor
user909
Posts: 4
Registered: ‎12-19-2011
Message 1 of 6 (569 Views)

Run awk script from within KSH script

Hello all,

 

Could someone please assist? I have an AWK routine the successfully prints a warning message for filesystem usage equal/greater than 90% when called as:  df -k | awk -f script.awk

 

I would like to call the entire routine from within a KORN shell script. I can't seen to get it to work, please see below:

 

df -k | awk '{

{
if($1=="Filesystem")
next
}

{
for(i=1;i<=NF;i++)
 if($i ~/%/)
 FLD=i

 if($FLD !~/-/)
 usage=sprintf("%d\n", substr($FLD,1,index($FLD,"%")-1)+0)

 if(usage>=90)
 printf("%s;%s%s%d%s\n", "Filesystem warning",$NF," usage at ",usage,"%")
}

}'

Spoiler
 

 

Any help would be appreciated, Thanks

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 2 of 6 (563 Views)

Re: Run awk script from within KSH script

[ Edited ]

>I would like to call the entire routine from within a KSH script. I can't seen to get it to work

 

What's failing, what errors do you get?

You also seem to have an extra level of {} that you can remove:

df -k | awk '

...

   printf("%s;%s%s%d%s\n", "Filesystem warning", $NF, " usage at ", usage, "%")
}'

 

And it would help if you had more indentation.

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 3 of 6 (548 Views)

Re: Run awk script from within KSH script

>I have an AWK routine the successfully prints a warning message

 

I'm not sure how?  I had to change your script to:

df -k | awk '
{
if ($1 == "Filesystem")
   next
if (substr($0, 1, 1) == "/")
   filesys=$1         # save filesystem name
FLD = -1
for (i=1; i<=NF; i++)
   if ($i ~ /%/) FLD=i

if (FLD == -1 || $FLD ~ /-/) next  # skip all but lines with "%"

usage = $(FLD - 1) + 0  # get previous field
if (usage >= 90)
   print "Filesystem warning;" filesys, "usage at", usage "%"
}'


Occasional Contributor
user909
Posts: 4
Registered: ‎12-19-2011
Message 4 of 6 (539 Views)

Re: Run awk script from within KSH script

Many thanks for the post. 

Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 5 of 6 (536 Views)

Re: Run awk script from within KSH script

Or you could use bdfmegs -qP 90

 

That will report nothing if no filesystems are 90% or larger, otherwise, one filesystem per line.

bdfmegs never splits long lines which makes it ideal for scripting.

 

Acclaimed Contributor
Dennis Handly
Posts: 25,043
Registered: ‎03-06-2006
Message 6 of 6 (533 Views)

Re: Run awk script from within KSH script

>Many thanks for the post.

 

If you are happy, please click on the Kudos star.

And if it is a solution, you can use the post Options mention to mark it as a solution.

The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.