Can't export variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can't export variable
# 1  
Old 01-17-2013
Can't export variable

I am relatively new to exporting variables, and I just can't seem to make this count work. What I have is the following:

Code:
TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1)|gawk '{print $6}'
RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,) |gawk '{print $6}'
export TOTALZ=`echo $TOTAL`
export RESTOREDZ=`echo $RESTORED`
count=$(($TOTALZ - $RESTOREDZ)); echo $count

The RESTORED and TOTAL variables return numerical values when the script is run. But when I try the count, the export doesn't work and I am left with an error message. What am I doing wrong? Any help is greatly appreciated. I can tell the variables are assigned to nothing because bash -x ./script1 shows that the export = nothing. Also I can't subtract these two values (TOTAL and RESTORED) without exporting as the debug shows they are unassigned as soon as they are calculated.
# 2  
Old 01-17-2013
Hi, a couple of questions:

- What do
Code:
$IMAGELIST -backupid $IM -U |gawk '{print $5}'

and
Code:
$BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,

produce?

- You can't have a pipe statement
Code:
|gawk '{print $6}'

after an assignment ( var= ... )

- Why do you feel you need to export those variables? It would not be required here..
- The use of echo is wrong, you would need to use echo "$TOTAL" (double quotes)
- Why are you using back ticks?, whereas 2 lines earlier you used $( .. ) which is a better construct...
- Why are you using echo? Why not assign directly:
Code:
TOTALz=$TOTAL

# 3  
Old 01-17-2013
I am still getting empty assignments:

Code:
 /tmp/jobtrail -imagereturn
 union_124444444 -U
++ gawk '{print $5}'
++ tail -1
+ TOTAL=4130577073
+ gawk '{print $6}'
++ /tmp/jobcmd -jobid 300999
++ cut -f15 -d,
+ RESTORED=3629672448
+ TOTALZ=
+ RESTOREDZ=

I am not sure it if is because of something I am missing from your post or not. The code now looks like this:

Code:
TOTALZ="$TOTAL"
RESTOREDZ="$RESTORED"
count=$(($TOTALZ - $RESTOREDZ)); echo $count

I am sure it is something dumb, but not sure what. The variables (as shown above) do have numerical values, then they will not be assigned later. Could this be because of gawk? or are there other issues?
# 4  
Old 01-17-2013
It's staring me in the face.

You've got pipes where they don't belong, and your brackets are closing too soon.

Code:
TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1)|gawk '{print $6}'
RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,) |gawk '{print $6}'

Code:
TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1|gawk '{print $6}')
RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d, |gawk '{print $6}' )

# 5  
Old 01-17-2013
Good Catch, almost worked!

It was a really good catch, Corona! It still does not work, but I am sure this was part of the error and as I was entering more code did not notice the brackets in the wrong place.

The RESTOREDZ is still empty as is the TOTALZ. I am investigating still. Thanks and if you find any further errors, and have the time, would greatly appreciate it if you would let me know!

Code:
TOTAL=$($IMAGELIST -backupid $IM -U |gawk '{print $5}' |tail -1|gawk '{print $6}')
RESTORED=$($BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d, |gawk '{print $6}')
TOTALZ="$TOTAL"
RESTOREDZ="$RESTORED"
count=$(($TOTALZ - $RESTOREDZ)); echo $count

---------- Post updated at 03:34 PM ---------- Previous update was at 03:20 PM ----------

The strange part is that now that I have placed the quotes where they SHOULD be, I can't even echo RESTORED or TOTAL. The TOTAL= is now blank, and before they had numbers. This must have something to do with the quotes or brackets, but as of yet, I am not sure what.
# 6  
Old 01-17-2013
Try removing the
Code:
|gawk '{print $6}'

bits, at least for the first statement if you first pipe through gawk '{print $5}' and then through gawk '{print $6}' you end up with nothing..

If that does not work either, please answer my question from post #2
This User Gave Thanks to Scrutinizer For This Post:
# 7  
Old 01-18-2013
Beginner's stupidity!

I double checked this script, and the entire problem revolved around the
Code:
[gawk '{print $6}'

.

This was actually NOT correct, and what happened was that if you ran the command without assigning it to a variable with the last gawk it would return NOTHING. However, if you ran the command as part of the variable, the variable assignment would somehow truncate the last gawk, and it would appear ok. But then the subtraction command would not pick it up, and would revert it to the true value, which was NOTHING.

I want to thank everyone for being so helpful and making such an effort to assist with this as it was human error totally!Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. Shell Programming and Scripting

export variable not working after su

I have a requirement to change user inside a shell script and execute group of commands. I have done it many times earlier but never came across the issue with exporting variables. Strangely if a var is exported afetr su, it is not working where as if it is does outside su, it works. Another issue... (8 Replies)
Discussion started by: sasiharitha
8 Replies

3. Shell Programming and Scripting

help t export the variable from a particular file

Hello Guys, I need you help to do one task I have script which is actually doing to fetch the code of any repository in svn for e.g.:- I can use svn to checkout the repository but I want to checkout the repository for particular tag like svn co <url>/svn/repo/<tag-name> and this... (1 Reply)
Discussion started by: rohit22hamirpur
1 Replies

4. Shell Programming and Scripting

where does variable export to?

Hi, Unix Gurus, I have a problem need help. I have a script to generate environment variable code same as following: oracle_sid=abcd export oracle_sid when I execute this code with command ./script_nane it succeeded. when I try to find it with env command or echo $oracle_sid, it does not show... (5 Replies)
Discussion started by: ken002
5 Replies

5. 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

6. Shell Programming and Scripting

Seeing variable which are exported with export

Greeting to all of you! I've small issue related to the variable which we are setting and exporting through scripts, in one of the script there are some variable used but I am not abel to get the detail as where they are set. I tried finding the detail with the help of env but no luck. ... (2 Replies)
Discussion started by: kumarmani
2 Replies

7. Shell Programming and Scripting

export variable question

simple question: for example if i use: export http_proxy=proxy:8080 and i have this script: while true; do .... lynx Google ;; wget The UNIX and Linux Forums - Learn UNIX and Linux from Experts ;; ... esac done So i must "export http_proxy=proxy:8080" before any lynx and wget... (4 Replies)
Discussion started by: aspire
4 Replies

8. 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

9. Shell Programming and Scripting

Export variable as number

Hi Guys, I am using the korn shell. I have an environments files where I have defined a variable export START_TIME=060000 export END_TIME=220000 I source this environments file into one of my scripts. The problem is that when I try to use this variable, I cannot get to use this variable as a... (2 Replies)
Discussion started by: zeus101
2 Replies

10. 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
Login or Register to Ask a Question