Columns Built in awk Variables don't export


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Columns Built in awk Variables don't export
# 1  
Old 03-04-2013
Columns Built in awk Variables don't export

I need to export the built in awk variables for columns so that they are available for the rest of the script. This is what I have so far:

Code:
cat /tmp/test | eval `awk '{print "export NAME="$1; print "export NAME2="$2;}'`
echo Host is "$NAME" and Host2 is "$NAME2"

When I run bash -x to debug, the variables do get assigned but then when the echo command runs they come up empty. I need to have these variables assigned so that the entire script can make use of them. Any ideas would be greatly appreciated.
# 2  
Old 03-04-2013
One approach is redirect output to a file and source the file, so that variables can be used further:
Code:
awk ' { print "export NAME1="$1; print "export NAME2="$2 }' /tmp/test > source_vars

. ./source_vars

echo "Host is $NAME1 and Host2 is $NAME2"


Last edited by Yoda; 03-04-2013 at 01:38 PM.. Reason: Removed chmod as it is not required. Thanks Scrutinizer!
# 3  
Old 03-04-2013
Why use awk? Try:
Code:
read NAME NAME2 < /tmp/tst
export NAME NAME2

--
@bipinajith, you do not need to chmod +x if you only source a file..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Export variables to subshells

So i have a script that is like this: #!/bin/sh VARA="elementary 1 elementary 2 nursery A nursery B highschool AZ" echo "${ContentOfADifferentSCRIPT}" | sh i do not have control over the script that is inside the variable "${ContentOfADifferentSCRIPT}". however, i know that the... (3 Replies)
Discussion started by: SkySmart
3 Replies

2. Shell Programming and Scripting

Read variables from a file and then export it.

Hi I want to read variables from one file and then set it as environment variable; The text file is test.txt which contains SPEED:1000 IP:172.26.126.11 My code is: while read line; do var1=`echo $line | awk 'BEGIN {FS=":"} { print $1 }'` echo $var1 var2=`echo $line | awk 'BEGIN {FS=":"}... (8 Replies)
Discussion started by: SSM
8 Replies

3. Shell Programming and Scripting

Export table of 50 columns

Dear all, I have a big table of 50 columns and more then 100, 000 rows in mysql. Could you please help me that how I can export it as .csv or . txt that I can open it in MS excel????? OR how can I export the specific columns of the table??? Thanks AAWT (5 Replies)
Discussion started by: AAWT
5 Replies

4. UNIX for Advanced & Expert Users

Bash script with export variables

Hi all guys, how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it. But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script. Consequently in... (8 Replies)
Discussion started by: idro
8 Replies

5. Shell Programming and Scripting

Crontab jobs don't see variables defined in /etc/profile

Is it true that Crontab jobs don't see variables defined in /etc/profile? How to get around that? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

6. Solaris

not able to export the Variables

I am working with Sun Solaris 9 and I want to export the environment variable from my application(xxxx.ksh) but I am not able to see it when I am using SET command I am writing some variables which I have to set COMMON_USER_HOME=${HOME} export COMMON_USER_HOME echo... (6 Replies)
Discussion started by: smartgupta
6 Replies

7. Shell Programming and Scripting

script to export variables

Hi, I am a newbie to unix as well as scripting. I need to write a script, which on execution sets the necessay oracle variables. Can someone help me out as to how to proceed? also can u suggest good tutorial for bash/shell scripting? thanks (1 Reply)
Discussion started by: aboxilica
1 Replies

8. Shell Programming and Scripting

export env variables

hi i want to write a shell script to set environment variables . But i am not been able to set that for the current shell instead i have to spawn a new shell. Is there a way to set the env variable for the current shell using shell script in bash shell ? Thnx (2 Replies)
Discussion started by: varun.81
2 Replies

9. Shell Programming and Scripting

export variables

I have a master shell, which calls another shell to export some env variables. But when I just run the child shell from the command line, and see if the variables are exported by doing, echo $EXPORTED_VAR1 I am not seeing the value. But I am sure, I am using the child shell from a master... (4 Replies)
Discussion started by: srishan
4 Replies

10. UNIX for Dummies Questions & Answers

awk built in variables

Dear experts I am learning awk command through some books on Solaris 8. I have tested the folloing command awk 'BEGIN { print match ("And" , /d/)}' then the result is as following awk: syntax error near line 1 awk: illegal statement near line 1 Could you please help on this and just... (4 Replies)
Discussion started by: Reza Nazarian
4 Replies
Login or Register to Ask a Question