Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

command line awk and if
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

Report this thread to moderator Post Follow-up to this message
Old Post
Faeandar
03-20-04 01:23 AM


Re: command line awk and if
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

Report this thread to moderator Post Follow-up to this message
Old Post
Bob Harris
03-20-04 01:23 AM


Re: command line awk and if
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\" " adm
in

# 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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
03-20-04 01:23 AM


Re: command line awk and if
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


Report this thread to moderator Post Follow-up to this message
Old Post
Faeandar
03-20-04 01:23 AM


Re: command line awk and if

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.


Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
03-20-04 01:23 AM


Re: command line awk and if
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Bill Marcum
03-20-04 01:23 AM


Re: command line awk and if
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

Report this thread to moderator Post Follow-up to this message
Old Post
Faeandar
03-20-04 01:23 AM


Re: command line awk and if
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
03-20-04 01:23 AM


Re: command line awk and if
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

Report this thread to moderator Post Follow-up to this message
Old Post
Faeandar
03-20-04 01:23 AM


Re: command line awk and if

>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

Report this thread to moderator Post Follow-up to this message
Old Post
Faeandar
03-20-04 01:23 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 11:32 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.