i have variable A which is an alias for variable B which is equal to "THIS IS A TEST"
when every i echo variable A i only get the alias name for variable B, NOT the contents of variable B.
HOSTNAME# echo $TESTIT
+ echo THIS IS A TEST
THIS IS A TEST
HOSTNAME# ls -l
total 16
-rw-r--r-- 1 user group 6 Jul 15 13:58 TEST.IT
HOSTNAME# for i in `ls`; do
DOH=`echo $i|awk -F. '{ print "$"$1$2 }'`
echo $DOH
done
$TESTIT
HOSTNAME#
I set A to equal value of B - echo $A - get nothing (B was nothing)
Set B and then A again - and the value shows up.
$ A=$B
$ B="This is a test"
$ echo $A
$ A=$B
$ echo $A
This is a test
As far as the code:
I don't see why you get two $TESTIT at the end. When I ran it, it only writes it out once ($TESTIT). I believe you are attempting to cat the file (show the contents of the file) Could you explain more of what you are attempting to do? What shell you are using and OS+version?
Realize you are not setting DOH to a value of another variable - you are setting it to "$" $1$2. This is two different things. Just as I stated earlier, if B is nothing setting A=$B give you nothing.
You are stating DOH to be equal to a string "$TESTIT" not a value of the variable $TESTIT. I'm not sure the work around (I've been looking...). Will get back to it tomorrow if no one else has (must go home as the EOD is here).
gsatch: i dont have the eval command. i will see if i can download it.
RTM: your help is much appricated. Yes, shortyly after i posted last night i went home also. and your answer makes complete sense; something i had not thought of.
I tried the eval command - Solaris 2.6 showed the following on the man page: eval - Evaluate a Tcl script. It didn't work and I'm not sure it would unless this was a Tcl script. But it was worth a shot. It also seems to me I have heard of this type of fix before - I just can't remember what/where. Will look at the script in a bit and post back.
'eval' is a built-in command in the borne, korn and c shells. Basically, it evaluates and executes whatever follows it as a command. In my example, '$a' is replaced by it's contents, or "b" so that the 'eval' line becomes the equivalent of 'echo $b'.
I did find in man -s 1 eval - which is what I was thinking about originally - don't know where the link to tcl came from (now I have a situtation to look into!).
Anyway, using the eval DOES work as suggested by gsatch. I put it into the test script I was running and it echos out the variable value.
#!/bin/ksh
for i in `ls`; do
DOH=`echo $i|awk -F. '{print $1$2}'`
echo "$DOH"
eval echo \$$DOH
done
You should not need to 'download' eval - it's a built-in function of the shell (which is why you may not find it with 'which eval').
Post back if you have problems with it - Way to go gsatch!