Re: awk: importing and exporting variables



On 2006-08-12, opexoc@xxxxxxxxx wrote:
Hi. I was trying to write script which will be do something when
program called kadu is executed. It is this script ( or some prototype
of this script ):

-------------------------------------------
#!/bin/bash
export var=0
ps aux | awk '/[k]adu/ { ENVIRON["var"]++ } END{ print ENVIRON["var"]
}'

echo $var
if [ $var = "1" ]
then
echo "1"
else
echo "0"
fi

-------------------------------------------

My main problem is to import content of variable "var" from awk. I
thought that when I will use "export" command and I make "var" some
global variable it will works, but it didn't. How can you help me ?

Awk variables are local to awk; they are not shell or environment
variables. If you want to use a value generated by awk in a shell
script, use command substitution:

var=$( ps aux | awk 'END { print NR }' )

Or, if you want to get more than one variable, have awk print a
shell assignment and use eval:

eval "$( ps aux | awk -v maxpid=-2 ' END { srand(); printf "var=%d\ntime=%d\n",
NR, srand() }' )"

--
Chris F.A. Johnson, author | <http://cfaj.freeshell.org>
Shell Scripting Recipes: | My code in this post, if any,
A Problem-Solution Approach | is released under the
2005, Apress | GNU General Public Licence
.



Relevant Pages

  • SUMMARY: Annoying Shell/Terminal Problem
    ... that a startup script was set to trapINT. ... # echo howdy^Cecho doody ... The ^C canceled the first echo, but it doesn't cause the shell ... when I run those echo commands from the shell. ...
    (SunManagers)
  • Re: [PATCH] Linux 2.6: shebang handling in fs/binfmt_script.c
    ... script files and their interpreters (shells, awk, perl, python, guile, ... that that shell or interpreter would be poorly ... And, to be truthful, the usual way that I code awk scripts is not as ...
    (Linux-Kernel)
  • Re: take file name from command line arguement and write to file
    ... the Bourne Again Shell. ... > echo $a ... splitting, parameter substitutions etc. ... script and first sees the eval command, at first it behaves the usual ...
    (comp.unix.shell)
  • Re: help regarding shell script to find out if a directory exists at a given path
    ... echo "found directory at the given path.." ... Every line I write is potentially a line in a script. ... resulting program will be easier to maintain than a bash script. ... shell is more than powerful enough. ...
    (comp.unix.programmer)
  • Re: Mutiple fork return status
    ... from a script you can obtain the exit code of any background process you spawned with the "wait" builtin, it will return the process' exit code as its own or 127 in case of errors try this sequence of commands, they are picked from memory but should be correct: ... # echo $? ... if you issue a "wait" while the process you are inquiring about is still running, the script will pause until the process in question exits by itself or is terminated. ... you can get more info about the "wait" builtin in the docs for the shell of your choice. ...
    (comp.os.linux.misc)