Languages and Scripting
cancel
Showing results for 
Search instead for 
Did you mean: 

Run awk script from within KSH script

user909
Occasional Contributor

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

5 REPLIES
Dennis Handly
Acclaimed Contributor

Re: Run awk script from within KSH script

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

Dennis Handly
Acclaimed Contributor

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 "%"
}'


user909
Occasional Contributor

Re: Run awk script from within KSH script

Many thanks for the post. 

Bill Hassell
Honored Contributor

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.

 



Bill Hassell, sysadmin
Highlighted
Dennis Handly
Acclaimed Contributor

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.