I am relatively new to exporting variables, and I just can't seem to make this count work. What I have is the following:
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.
Hi, a couple of questions:
$IMAGELIST -backupid $IM -U |gawk '{print $5}'
and
$BPDBJOBS -most_columns -jobid $JOBS |cut -f15 -d,
produce?
- You can't have a pipe statement
|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:
TOTALz=$TOTAL
I am still getting empty assignments:
/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:
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?
It's staring me in the face.
You've got pipes where they don't belong, and your brackets are closing too soon.
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}'
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}' )
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!
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.
Try removing the
|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
I double checked this script, and the entire problem revolved around the
[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!
Hi, glad you resolved it. What was happening here is slightly different still:
In the first case, the commands before the | are executed in a subshell, and therefore variable assignments made there will not persist in the main shell. The assignment will show up with the set -x option, but it will not persist.
In the second case the pipeline is part of the assignment inside a command substitution, so it will persist but gawk '{print $6}' filtered out the value, so what got persistently stored in the variable was an empty value.