Re: awk: importing and exporting variables
- From: "Chris F.A. Johnson" <cfajohnson@xxxxxxxxx>
- Date: Sat, 12 Aug 2006 12:41:00 -0400
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
.
- References:
- awk: importing and exporting variables
- From: opexoc
- awk: importing and exporting variables
- Prev by Date: Re: I really need some help here udev/hotplug/usb/usbmount etc etc.
- Next by Date: Re: Opening Kuickshow in a given directory
- Previous by thread: awk: importing and exporting variables
- Next by thread: Re: awk: importing and exporting variables
- Index(es):
Relevant Pages
|