Help answer threads with 0 replies.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 02-22-2010, 09:01 AM   #1
LQ Newbie
Registered: Feb 2010
Posts: 1

Rep: Reputation: 0
Unhappy Shell variable in awk script

I need to export some Varibales in Shell like (KSH )

export CORE=1
export DODDY=0

then I need to call these variables in the awk

#!/bin/awk -f


if ( $CORE ~ "1" ) {

print "Core is right one "


But awk doesnot understand the variables and I am not getting the right results

Can you help me out

Old 02-22-2010, 09:15 AM   #2
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,577
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Try awk's -v option
Old 02-22-2010, 09:17 AM   #3
Registered: Jan 2010
Location: the universe
Distribution: Slackware (modified), Slackware64 (modified), openSuSE (modified)
Posts: 342

Rep: Reputation: 62
In awk the environment variables can be accessed through the associative array ENVIRON:
#!/bin/awk -f


if ( ENVIRON["CORE"] ~ "1" ) {

print "Core is right one "

Old 02-22-2010, 09:32 AM   #4
LQ Veteran
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quick glance:

The -f flag says to get code from a file, but you have the code inline.

"~" means "ERE match". Why not just use "==" ?


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Trouble storing a variable in a shell script after awk output uncle-c Linux - Newbie 3 02-08-2010 08:13 AM
awk and shell variable markux Linux - Server 6 01-19-2009 04:54 AM
passing awk output to $variable for shell script? NewnanNOC Programming 3 10-23-2008 03:08 PM
How to share variable values between shell and awk? realos Programming 1 12-16-2006 11:15 PM
Accessing Shell variable in awk script dileepkk Linux - General 1 10-07-2004 08:47 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:14 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration