Unix Shell - how to update shell vars within awk script

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > November 2006 > how to update shell vars within awk script





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 how to update shell vars within awk script
Jerry Fleming

2006-11-22, 7:27 am

I see many example using shell vars within gawk scripts, not UPDATING.

What i want is to check disk space and, update the var in the shell
[code]
missing=nothing
ssh root@nadal df | gawk '
BEGIN {
disk=""
}
/\/mnt/ {
if( int($5) >= 10 ) disk = disk ":" $6
}
END {
if(disk)
{
'"$missing"'='"$missing"' "nadal::" disk
exit 1
}
else exit 0
}
'
echo $missing
[/code]
so that if a mounted disk is nearly used up, a message will be printed.
Any hint will be heartily appreciated.

-Jerry
Janis Papanagnou

2006-11-22, 7:27 am

Jerry Fleming wrote:
> I see many example using shell vars within gawk scripts, not UPDATING.
>
> What i want is to check disk space and, update the var in the shell


You can't update shell variables from the calling shell through a
called awk program. Though you can let awk print the data and let
the shell update it...

eval $( awk '{ print "missing="somevalue }' )

> [code]
> missing=nothing
> ssh root@nadal df | gawk '
> BEGIN {
> disk=""
> }
> /\/mnt/ {
> if( int($5) >= 10 ) disk = disk ":" $6
> }
> END {
> if(disk)
> {
> '"$missing"'='"$missing"' "nadal::" disk
> exit 1
> }
> else exit 0
> }
> '
> echo $missing
> [/code]
> so that if a mounted disk is nearly used up, a message will be printed.
> Any hint will be heartily appreciated.


If you just want to print it let the awk print it. If you need the
value also in the shell use the above outlined method.

Janis

>
> -Jerry

Jerry Fleming

2006-11-22, 7:27 am

On 2006-11-22 17:30, the carbon-based lifeform Janis Papanagnou inspired
saybotees with:[vbcol=seagreen]
> Jerry Fleming wrote:
>
> You can't update shell variables from the calling shell through a
> called awk program. Though you can let awk print the data and let
> the shell update it...
>
> eval $( awk '{ print "missing="somevalue }' )
>
>
> If you just want to print it let the awk print it. If you need the
> value also in the shell use the above outlined method.
>
> Janis
>
yeah, that works! thanks a lot!
i tried to put the awk script in a subshell and pipe the output to bash,
but it failed. i should pipe the output to eval, instead.

(df | awk '...') | eval

am i right?
Janis Papanagnou

2006-11-22, 7:27 am

Jerry Fleming wrote:
> On 2006-11-22 17:30, the carbon-based lifeform Janis Papanagnou inspired
> saybotees with:
>
>
> yeah, that works! thanks a lot!
> i tried to put the awk script in a subshell and pipe the output to bash,
> but it failed. i should pipe the output to eval, instead.
>
> (df | awk '...') | eval
>
> am i right?


Rather...

eval $( df | awk '...' )

....I'd say.

Janis
Bill Marcum

2006-11-22, 7:27 am

On Wed, 22 Nov 2006 17:57:44 +0800, Jerry Fleming
<jerry@dd.comlk> wrote:
> On 2006-11-22 17:30, the carbon-based lifeform Janis Papanagnou inspired
> saybotees with:
> yeah, that works! thanks a lot!
> i tried to put the awk script in a subshell and pipe the output to bash,
> but it failed. i should pipe the output to eval, instead.
>
> (df | awk '...') | eval
>
> am i right?


If you use a pipe, eval will be in a subshell unless your shell is ksh or
zsh. And there is the difference between command line arguments and
standard input.


--
Humility is the first of the virtues -- for other people.
-- Oliver Wendell Holmes
Stephane CHAZELAS

2006-11-22, 7:27 am

2006-11-22, 05:42(-05), Bill Marcum:
[...]
>
> If you use a pipe, eval will be in a subshell unless your shell is ksh or
> zsh. And there is the difference between command line arguments and
> standard input.

[...]

Only some versions of ksh though: the zsh and AT&T based ones,
not the pdksh based ones.

--
Stéphane
Ed Morton

2006-11-22, 1:17 pm

Jerry Fleming wrote:

> I see many example using shell vars within gawk scripts, not UPDATING.
>
> What i want is to check disk space and, update the var in the shell
> [code]
> missing=nothing
> ssh root@nadal df | gawk '
> BEGIN {
> disk=""
> }
> /\/mnt/ {
> if( int($5) >= 10 ) disk = disk ":" $6
> }
> END {
> if(disk)
> {
> '"$missing"'='"$missing"' "nadal::" disk
> exit 1
> }
> else exit 0
> }
> '
> echo $missing
> [/code]
> so that if a mounted disk is nearly used up, a message will be printed.
> Any hint will be heartily appreciated.
>
> -Jerry


awk is not shell. You shouldn't expect to be able to update a shell
variable within awk any more than you would within C. The syntax you're
using above to reference the value of shell variables from within awk is
error-prone and should be avoided (see
http://home.comcast.net/~j.p.h/cus-faq-2.html#24). It's also misleading
as it makes it appear as if you COULD change the variables value. You
don't need to init awk variables so the BEGIN section is redundant. You
don't do anything with the exit code so you don't really need that.
Given that, your script could be rewritten as just:

[code]
missing=nothing
ssh root@missing=`nadal df | gawk -v missing="$missing" '
/\/mnt/ && (int($5) >= 10) { disk = disk ":" $6 }
END { if(disk) print missing "nadal::" disk }'`
echo "$missing"
[/code]

I don't see why you want the word "nothing" in the result if "disk" is
populated, so that may be a mistake in your script. What I suspect you
really want is:

[code]
ssh root@missing=`nadal df | gawk '
/\/mnt/ && (int($5) >= 10) { disk = disk ":" $6 }
END { print (disk ? "nadal::" disk : "nothing" }'`
echo "$missing"
[/code]

Regards,

Ed.
SiKing

2006-11-22, 1:17 pm

Jerry Fleming wrote:
> I see many example using shell vars within gawk scripts, not UPDATING.
>
> What i want is to check disk space and, update the var in the shell
> [code]
> missing=nothing
> ssh root@nadal df | gawk '
> BEGIN {
> disk=""
> }
> /\/mnt/ {
> if( int($5) >= 10 ) disk = disk ":" $6
> }
> END {
> if(disk)
> {
> '"$missing"'='"$missing"' "nadal::" disk
> exit 1
> }
> else exit 0
> }
> '
> echo $missing
> [/code]
> so that if a mounted disk is nearly used up, a message will be printed.
> Any hint will be heartily appreciated.
>
> -Jerry


I seem to have ran into a similar problem a little while back. The answer that I
got from the developers of awk is here
<http://groups.google.com/group/gnu....a5f9e2e8fcc293d>; it might be
of some use to you.

HTH.

--
mean people suck, naughty people swallow :o
-----
Candy for spammers:
http://members.shaw.ca/grubb/spamthis.html
Jerry Fleming

2006-11-22, 7:22 pm

On 2006-11-22 22:59, the carbon-based lifeform Ed Morton inspired
comp.unix.shell with:
> Jerry Fleming wrote:
>
>
> awk is not shell. You shouldn't expect to be able to update a shell
> variable within awk any more than you would within C. The syntax you're
> using above to reference the value of shell variables from within awk is
> error-prone and should be avoided (see
> http://home.comcast.net/~j.p.h/cus-faq-2.html#24). It's also misleading
> as it makes it appear as if you COULD change the variables value. You
> don't need to init awk variables so the BEGIN section is redundant. You
> don't do anything with the exit code so you don't really need that.
> Given that, your script could be rewritten as just:
>
> [code]
> missing=nothing
> ssh root@missing=`nadal df | gawk -v missing="$missing" '
> /\/mnt/ && (int($5) >= 10) { disk = disk ":" $6 }
> END { if(disk) print missing "nadal::" disk }'`
> echo "$missing"
> [/code]
>
> I don't see why you want the word "nothing" in the result if "disk" is
> populated, so that may be a mistake in your script. What I suspect you
> really want is:
>
> [code]
> ssh root@missing=`nadal df | gawk '
> /\/mnt/ && (int($5) >= 10) { disk = disk ":" $6 }
> END { print (disk ? "nadal::" disk : "nothing" }'`
> echo "$missing"
> [/code]
>
> Regards,
>
> Ed.


Actually, this is only part of a lengthy script; the exit status is
needed by further processing, and the word 'nothing' is necessary. Yes,
the BEGIN section is redundant, since variables are initialized
automatically.
Thank you, Ed!
Ed Morton

2006-11-23, 1:29 am

Jerry Fleming wrote:
> On 2006-11-22 22:59, the carbon-based lifeform Ed Morton inspired
> comp.unix.shell with:
>
>
>
> Actually, this is only part of a lengthy script; the exit status is
> needed by further processing, and the word 'nothing' is necessary.


Then all you need is:

[code]
ssh root@missing=`nadal df | gawk '
/\/mnt/ && (int($5) >= 10) { disk = disk ":" $6; found = 1 }
END { print "nothing" (found ? "nadal::" disk : ""); exit found }'`
echo "$missing"
[/code]

Ed.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com