1753540 Members
5235 Online
108795 Solutions
New Discussion юеВ

Awk syntax

 
SOLVED
Go to solution
Mike Gordon
Occasional Advisor

Awk syntax

I have a small script that is used to monitor and warn myself if certain filesystems exceed a set capacity ie more than 80% used etc.

This is currently done using the following syntax :

space=`bdf | awk '$6 == "/" {print $5}' | cut -f1 -d"%"`

I would like to amend the script so filesystems are picked up from a configuration file instead of being hardcoded. Intended syntax is something like :

for fs in `cat filesystems`
do
space=`bdf | awk '$6 == $fs {print $5}' | cut -f1 -d"%"`

etc

Appears that value for $fs is not being determined. Awk fails with :

awk: Field $() is not correct.
The input line number is 1.
The source line number is 1.

Any ideas much appreciated.

Mike
10 REPLIES 10
Andreas Voss
Honored Contributor
Solution

Re: Awk syntax

Hi,

within your awk $fs is not known because it's a shell variable.
To tell awk the variable $fs use:
space=`bdf | awk -v fs=$fs '$6 == $fs {print $5}' | cut -f1 -d"%"`

Regards

Andrew
James R. Ferguson
Acclaimed Contributor

Re: Awk syntax

Mike:

Do this:

Make your /tmp/stuff file like this:

/tmp
/var
/home

Then:

for fs in `cat /tmp/stuff`
do
space=`bdf|awk -v fs=$fs '$6==fs {print $5}'|cut -f1 -d"%"`
done

...adding whatever you want in the do-loop.

...JRF...
Andreas Voss
Honored Contributor

Re: Awk syntax

Sorry, if you assign a variable with -v var=value the within awk you must not use the $ before that var:
space=`bdf | awk -v fs=$fs '$6 == fs {print $5}' | cut -f1 -d"%"`


James R. Ferguson
Acclaimed Contributor

Re: Awk syntax

Mike:

Do this:

Make your /tmp/stuff file like this:

/tmp
/var
/home

Then:

for fs in `cat /tmp/stuff`
do
space=`bdf|awk -v fs=$fs '$6==fs {print $5}'|cut -f1 -d"%"`
done

...adding whatever you want in the do-loop.

...JRF...
John Palmer
Honored Contributor

Re: Awk syntax

You could also use the fact that bdf will take a filesystem as an argument:-

for fs in $(do
space=$(bdf ${fs} | tail -1|awk '{print $5}')
space=${space%%\%}
etc etc
done
John Palmer
Honored Contributor

Re: Awk syntax

Or without using 'tail':-

for fs in $(do
space=$(bdf ${fs} | awk '{if ($1 ~ /\//) {print $5}}')
space=${space%%\%}
etc etc...
done

Re: Awk syntax

Hi!

Use this script...

.....

current_mounted_fs=`bdf | grep "/" |
awk '{if (NF == 6) {print $6}};
(NF == 5) {print $5}'`

for fs in $current_mounted_fs
do
used_rate=`bdf $fs | grep $fs
| awk '{if (NF == 6) {print $5}};
(NF == 5) {print $4}'
| awk -F% '{print $1}'`
if [ $used_rate -gt 80 ] ; then
echo "Waring! $fs used $used_rate% ..."
fi
done

.....
Werner Vergauwen
New Member

Re: Awk syntax

Bear in mind though the "-v" parameter is an extension to the awk syntax, and might not work on all platforms.

So you could also use double quotes in stead of single, like so :
space=`bdf | awk "$6 == $fs {print $5}" | cut -f1 -d"%"`

cheers
Fred Martin_1
Valued Contributor

Re: Awk syntax

Here's a fragment that I wrote for my own use. As written it alerts you when any file system is 90% or greater capacity:

if [ "`/usr/bin/bdf | grep dev | /usr/bin/awk '{print $5}' | /usr/bin/sed 's/\%//g' | /usr/bin/awk '
{if($0>=90){print "OVER"}}
'`" != "" ]
then
# here you can send email or whatever
fi
fmartin@applicatorssales.com