Need to assign value to a variable and export it at the same time

 
Thread Tools Search this Thread
Operating Systems Linux SuSE Need to assign value to a variable and export it at the same time
# 1  
Old 11-07-2012
Need to assign value to a variable and export it at the same time

I have a file a.xml contaning:

<customerId>999</customerId>
<aaa>09876</aaa>

Now I want to extract the value '999' and save it in a variable and then export the variable. Something like the below:

export CUSTOMER_ID=cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}'

Whereas
cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}' is giving the expected result i.e. 999

But when I want to save it in a variable, it is not working Smilie. Please help me urgently. My Operating System is SUSE Linux.
# 2  
Old 11-07-2012
Hi

Your command is correct. You just need to use backticks:

Code:
export CUSTOMER_ID=`cat a.xml |grep customerId |awk -F ">" '{print $2}' |awk -F "<" '{print $1}'`

Also, you can simplify this command like this:

Code:
 export CUSTOMER_ID=`awk -F "[><]" '/customerId/{print $3}' a.xml`

Guru.
# 3  
Old 11-07-2012
backtick operator missing.
(also avoid useless cat usage Smilie )

Code:
export CUSTOMER_ID=`grep customerId a.xml|awk -F ">" '{print $2}' |awk -F "<" '{print $1}'`

# 4  
Old 11-07-2012
Thank you Guru and Ningy. I can now save and export it.

But how do I use the value of this exported variable from outside of the script.

i.e. when I exit the script and do a echo $CUSTOMER_ID, I do not see '999'.

---------- Post updated at 02:15 AM ---------- Previous update was at 01:57 AM ----------

Well .. now I can use the exported value globally by running script like below

. tool.sh
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How can I assign awk's variable to shell script's variable?

I have the following script, and I want to assign the output ($10 and $5) from awk to N and L: grdinfo data.grd | awk '{print $10,$5}'| read N L output from gridinfo data.grd is: data.grd 50 100 41 82 -2796 6944 0.016 0.016 3001 2461. where N and L is suppose to be 3001 and 100. I use... (8 Replies)
Discussion started by: geomarine
8 Replies

2. UNIX for Beginners Questions & Answers

Need to pass variable in a command and assign value to a variable

Hello All, Hope you're doing well ! I am trying below command to be passed in a shell script, header_date_14 is a variable and $1 is the name of a file I intend to pass as a command line argument, however command line argument is not being accepted. header_date_14=$(m_dump... (8 Replies)
Discussion started by: ektubbe
8 Replies

3. UNIX for Advanced & Expert Users

Need help export variable

Hi, Please find my code below. ps -xef | grep 14766 | awk 'BEGIN{X="java_home=";X="weblogic_home="} {for(i=1;i<=NF;i++){if($i ~ /-Dplatform\.home|java$/){split($i,P,"=");s=P?P:$i;print X""s}}}' echo "java_home="$java_home echo "weblogic_home="$weblogic_home Output: Why does... (3 Replies)
Discussion started by: mohtashims
3 Replies

4. Shell Programming and Scripting

Export variable

Hi I have a pass a variable from one script to another. Here are my scripts Script #1 ./profile #!/bin/sh export NAME="Hello" Script #2 ./test #!/bin/sh ./profile echo $NAME when I run ./test .. i am not getting anything .. why is that? (5 Replies)
Discussion started by: arex876
5 Replies

5. Shell Programming and Scripting

Shell assign variable to another variable

How can I assign a variable to an variable. IE $car=honda One way I can do it is export $car=honda or let $car=2323 Is there any other ways to preform this task (3 Replies)
Discussion started by: 3junior
3 Replies

6. Shell Programming and Scripting

assign awk's variable to shell script's variable?

Dear All, we have a command output which looks like : Total 200 queues in 30000 Kbytes and we're going to get "200" and "30000" for further process. currently, i'm using : numA=echo $OUTPUT | awk '{print $2}' numB=echo $OUTPUT | awk '{print $5}' my question is : can I use just one... (4 Replies)
Discussion started by: tiger2000
4 Replies

7. Shell Programming and Scripting

Export Variable

How to export variable from one script to other? Can anybody give me syntax for that? Thanks (2 Replies)
Discussion started by: navi
2 Replies

8. UNIX for Dummies Questions & Answers

Export command giving Variable Name vs the Value set for the Variable

I'm having an issue when I export within my program. I'm getting the variable name, not the variable value. I have a configuration file (config.txt) that has the values of the variables set as so: set -a export ARCHIVEPOSourceDir="/interfaces/po/log /interfaces/po/data" export... (2 Replies)
Discussion started by: ParNone
2 Replies

9. Shell Programming and Scripting

Assign time value

I'm trying to monitor the traffic for few hours but still don't know how to make it possible. I try to set the value of time HH MM SS each into variables but for Hour, there's an error occurred as follow. typeset -i hour=$(date +%H) typeset: 09: value too great for base (error token is "09") ... (2 Replies)
Discussion started by: phragix
2 Replies
Login or Register to Ask a Question