For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2004 > command line awk and if









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author command line awk and if
Faeandar

2004-03-19, 8:23 pm

I'm trying to write a script that that will check space on certain
partitions, if it's less than a certain amount it will exit and
e-mail, if it's greater than that amount it will continue in the
script.

I'm looking to use df, tail, and awk for the first function ina manner
similar to:

df -k | tail +2 | awk 'stuff'

The script is for Solaris 2.6 and above.

The 'stuff' part involves an if statement. According to the book it
works very much like normal sh if statements:

if ( $4<300000)
{
mailx -s "Target partition is too small to install agent"
$admin
exit 1
else
continue
}

Two things from this.

1) am I conceptually in left field or does this look correct?

2) what is the syntax for this inside the script?
is this entire statmement inside the two ticks of the awk
command?

Thanks.

~F
Bob Harris

2004-03-19, 8:23 pm

In article <eh6j201iuj9auvcok1qjde73ti1vtnqajs@4ax.com>,
Faeandar <nospam@nospam.com> wrote:

> I'm trying to write a script that that will check space on certain
> partitions, if it's less than a certain amount it will exit and
> e-mail, if it's greater than that amount it will continue in the
> script.
>
> I'm looking to use df, tail, and awk for the first function ina manner
> similar to:
>
> df -k | tail +2 | awk 'stuff'
>
> The script is for Solaris 2.6 and above.
>
> The 'stuff' part involves an if statement. According to the book it
> works very much like normal sh if statements:
>
> if ( $4<300000)
> {
> mailx -s "Target partition is too small to install agent"
> $admin
> exit 1
> else
> continue
> }
>
> Two things from this.
>
> 1) am I conceptually in left field or does this look correct?
>
> 2) what is the syntax for this inside the script?
> is this entire statmement inside the two ticks of the awk
> command?
>
> Thanks.
>
> ~F


awk -v admin=$admin '
$4 < 300000 {
cmd = "mailx -s "Target partition is too small to install agent"
cmd = cmd admin # concontate admin string to mailx command
system(cmd)
exit(1)
}
'

that should do it.

Bob Harris
Patrick TJ McPhee

2004-03-19, 8:23 pm

In article <eh6j201iuj9auvcok1qjde73ti1vtnqajs@4ax.com>,
Faeandar <nospam@nospam.com> wrote:

% I'm looking to use df, tail, and awk for the first function ina manner
% similar to:

I'd be inclined to drive this from the awk program, without using tail.

BEGIN {
dfcmd = "df -k"
mailcmd = "mailx -s \"Target partition is too small to install agent\" " admin

# this is equivalent to df -k | tail +2
dfcmd | getline
dfcmd | getline

# this reads the next line from the pipeline until there is no next
# line, and sets $0, $1, et al
while ((dfcmd | getline) > 0) {
if ($4 < 300000) {
# we print $0 1. so that mailx doesn't hang waiting for input and
# 2. so that admin knows which partition is too small
# the | sets up a pipeline to mailcmd
print | mailcmd

# and we must close commands. In this case, the mail wouldn't be
# sent until we exit from awk (granted we're about to do that).
close(mailcmd)
exit(1)
}
}

# In this case, if we wanted to run df again, we would get an eof
# right away if we didn't close the command this time
close(dfcmd)
}

--

Patrick TJ McPhee
East York Canada
ptjm@interlog.com
Faeandar

2004-03-19, 8:23 pm

Regrettably neither post is working. Running Bob's I get

awk: syntax error near line 1
awk: bailing out near line 1

I haven't been able to find a reference to -v in the man page or in my
book but I have tried removing that and the admin=$admin (putting the
actual mail address inside the awk script) and that fails as well.

Running Patrick's I get

awk: syntax error near line 7
awk: illegal statement near line 7
awk: illegal statement near line 8
awk: syntax error near line 12
awk: illegal statement near line 12
awk: syntax error near line 14
awk: illegal statement near line 14
../spc_unix_install.sh: print: not found
../spc_unix_install.sh: mailcmd: not found
../spc_unix_install.sh: syntax error at line 39: `close' unexpected

I put dfcmd = "df -k /opt" in the first line to make it legal but the
above is what comes out.

Any other suggestions? I've been reading this awk book and not making
much sense of it.

Thanks.

~F

On Wed, 11 Feb 2004 03:05:03 GMT, Faeandar <nospam@nospam.com> wrote:

>I'm trying to write a script that that will check space on certain
>partitions, if it's less than a certain amount it will exit and
>e-mail, if it's greater than that amount it will continue in the
>script.
>
>I'm looking to use df, tail, and awk for the first function ina manner
>similar to:
>
>df -k | tail +2 | awk 'stuff'
>
>The script is for Solaris 2.6 and above.
>
>The 'stuff' part involves an if statement. According to the book it
>works very much like normal sh if statements:
>
>if ( $4<300000)
>{
> mailx -s "Target partition is too small to install agent"
>$admin
> exit 1
>else
> continue
>}
>
>Two things from this.
>
>1) am I conceptually in left field or does this look correct?
>
>2) what is the syntax for this inside the script?
> is this entire statmement inside the two ticks of the awk
>command?
>
>Thanks.
>
>~F


Ed Morton

2004-03-19, 8:23 pm



Faeandar wrote:
> Regrettably neither post is working. Running Bob's I get
>
> awk: syntax error near line 1
> awk: bailing out near line 1


There's a missing " at the end of the first cmd line. Also, some awks
don't support -v and I THINK I've had problems with putting the 's on a
separate line too, try this instead if just fixing the double quotes
doesn't work:

awk '$4 < 300000 {
cmd = "mailx -s \"Target partition is too small to install agent\""
cmd = cmd admin # concontate admin string to mailx command
system(cmd)
exit(1)
}' admin="$admin"

<snip>

> Any other suggestions? I've been reading this awk book and not making
> much sense of it.


Maybe you should get a newer awk (e.g. from GNU).

Ed.

Bill Marcum

2004-03-19, 8:23 pm

On Thu, 12 Feb 2004 02:27:52 GMT, Faeandar
<nospam@nospam.com> wrote:
> Regrettably neither post is working. Running Bob's I get
>
> awk: syntax error near line 1
> awk: bailing out near line 1
>
> I haven't been able to find a reference to -v in the man page or in my
> book but I have tried removing that and the admin=$admin (putting the
> actual mail address inside the awk script) and that fails as well.
>

If you are using Solaris, use nawk or /usr/xpg4/bin/awk.


--
Stay away from hurricanes for a while.
Faeandar

2004-03-19, 8:23 pm

On Thu, 12 Feb 2004 09:03:33 -0600, Ed Morton <morton@lsupcaemnt.com>
wrote:

>
>
>Faeandar wrote:
>
>There's a missing " at the end of the first cmd line. Also, some awks
>don't support -v and I THINK I've had problems with putting the 's on a
>separate line too, try this instead if just fixing the double quotes
>doesn't work:
>
>awk '$4 < 300000 {
> cmd = "mailx -s \"Target partition is too small to install agent\""
> cmd = cmd admin # concontate admin string to mailx command
> system(cmd)
> exit(1)
> }' admin="$admin"
>
><snip>
>
>
>Maybe you should get a newer awk (e.g. from GNU).
>
> Ed.


I tried what you mentioned but no love. I took Bill's advice and used
nawk and viola, it worked (to a point).

Apparently nawk (awk?) does not like using outside variables in a
statement. Also, concatenating cmd's did not appear to work. I've
changed it to the following:

df -k /opt | tail +2 | nawk '$4 < 30000000 {
cmd = "mailx -s \"Target partition is too small to install
agent\" $mail"
system(cmd)
exit(1)
}' admin="$admin"

When I replace $mail with an actual username it works nicely. And yes
mail= is defined at the beginning, I'm not that much of a beginner....

It spits out this when I run it with anything but a username for
mailx.

The flags you gave are used only when sending mail.
Usage: mailx -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address
-s SUBJECT -f FILE users

I can make this work and appreciate all the help. Of course if anyone
has answers to the little nagging aspects of this I would love to
learn how to make it work.

Thanks.

~F
Kenny McCormack

2004-03-19, 8:23 pm

In article <hfca30pfq69b3ladjracchcsdn0dfk5rnd@4ax.com>,
Faeandar <nospam@nospam.com> wrote:
....
>
>I tried what you mentioned but no love. I took Bill's advice and used
>nawk and viola, it worked (to a point).


Never use /usr/bin/awk under Solaris.

>Apparently nawk (awk?) does not like using outside variables in a
>statement. Also, concatenating cmd's did not appear to work. I've
>changed it to the following:
>
>df -k /opt | tail +2 | nawk '$4 < 30000000 {
> cmd = "mailx -s \"Target partition is too small to install
>agent\" $mail"
> system(cmd)
> exit(1)
> }' admin="$admin"


Blech! Why not just do it all in shell - something like:

# Yes, this is O/T in c.l.a. (see below)
[ $(df -k /opt | tail +2) -lt 30000000 ] && {
mailx ...
exit 1
}

>When I replace $mail with an actual username it works nicely. And yes
>mail= is defined at the beginning, I'm not that much of a beginner....
>
>It spits out this when I run it with anything but a username for
>mailx.
>
>The flags you gave are used only when sending mail.
>Usage: mailx -eiIUdFntBNHvV~ -T FILE -u USER -h hops -r address
> -s SUBJECT -f FILE users
>
>I can make this work and appreciate all the help. Of course if anyone
>has answers to the little nagging aspects of this I would love to
>learn how to make it work.


At first glance, it looks like a shell-quoting issue (the usual "Why can't
I use $foo in my AWK scripts?" - i.e., a confusion between AWK & shell,
which, believe it or not, are two separate, and distinct programming
languages), but looking at it more closely, it looks like it actually
should work, provided the (shell) variable "mail" is correctly set and
exported before the script is run.

Anyway, take it to comp.lang.shell - they'll be more than happy to assist.

Faeandar

2004-03-19, 8:23 pm

Xref: kermit comp.lang.awk:19726


>
>Never use /usr/bin/awk under Solaris.


Got it.

>
>
>Blech! Why not just do it all in shell - something like:
>
># Yes, this is O/T in c.l.a. (see below)
>[ $(df -k /opt | tail +2) -lt 30000000 ] && {
> mailx ...
> exit 1
> }


syntax error at line 5: `(' unexpected
Should this be an if statement? Or is that what "$" is supposed to do
in this case?

>
>
>At first glance, it looks like a shell-quoting issue (the usual "Why can't
>I use $foo in my AWK scripts?" - i.e., a confusion between AWK & shell,
>which, believe it or not, are two separate, and distinct programming
>languages), but looking at it more closely, it looks like it actually
>should work, provided the (shell) variable "mail" is correctly set and
>exported before the script is run.


I know enough to know that shell and awk a re different, I just wanted
to try it in case....

>
>Anyway, take it to comp.lang.shell - they'll be more than happy to assist.


Ok, thanks for the help so far.

~F
Faeandar

2004-03-19, 8:23 pm


>Anyway, take it to comp.lang.shell - they'll be more than happy to assist.


I don't freakin believe it! Comp.lang.shell is not a group on my news
server....

At least I have comp.unix.shell but still. What a crock.

~F
Kenny McCormack

2004-03-19, 8:23 pm

In article <3qga30h1lhfur2f3h8v8vv4615hblmfbe0@4ax.com>,
Faeandar <nospam@nospam.com> wrote:
>
>
>I don't freakin believe it! Comp.lang.shell is not a group on my news
>server....
>
>At least I have comp.unix.shell but still. What a crock.
>
>~F


Yes, typo (well, 4 of them) on my part. You can see that I really don't
test before I post.

Anyway, to speak to your other question(s):

$(...) is the modern (Posix) shell equivalent of `...`
If you shell doesn't like it (probably because you are using /bin/sh under
Solaris - did I mention, never use /bin/sh under Solaris...?), you can
either revert to backticks or start using a modern shell (I use /bin/ksh
under Solaris). Note that by "modern", I don't mean bleeding edge, not by
a long shot.

And, on the other front, [ ... ] && ... works, even in /bin/sh under
Solaris (!).

Faeandar

2004-03-19, 8:23 pm


>
>$(...) is the modern (Posix) shell equivalent of `...`
>If you shell doesn't like it (probably because you are using /bin/sh under
>Solaris - did I mention, never use /bin/sh under Solaris...?), you can
>either revert to backticks or start using a modern shell (I use /bin/ksh
>under Solaris). Note that by "modern", I don't mean bleeding edge, not by
>a long shot.


You may have mentioned something like that.....

I actually use tcsh but the shell directive in the script is
#!/bin/sh. Old habit I guess. Of course I need to learn the
translations now thankyouverymuch.

>
>And, on the other front, [ ... ] && ... works, even in /bin/sh under
>Solaris (!).


~F
irish

2004-03-19, 8:23 pm

Try this.

df -k | tail +2 | \
nawk '{ if ( $4 < 6000000 ) {
cmd = "mailx -s \"Target partition is too small to install agent\""
cmd = cmd" "admin # concontate admin string to mailx command
print cmd
system(cmd)
exit(1)
}
}' admin=admin

nawk is powerful!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com