Passing a variable name in a variable


 
Thread Tools Search this Thread
Operating Systems AIX Passing a variable name in a variable
# 1  
Old 05-21-2008
Passing a variable name in a variable

I am having a problem passing a variable as a variable. I am reading a file that contains the value “$BAKDIR”. I want UNIX to recognize it as a variable, but it’s treating it like a string.

Here is an example

#!/bin/sh
BAKDIR=$HOME/;export BAKDIR
echo "BAKDIR = " "$BAKDIR"
#------------------------------------------------------------------
# 1 - Get File names and paths to be cleaned up #
#-----------------------------------------------------------
while read p1 p2 p3
do
echo "$p1" " - " "$p2" " - " "$p3 "
p4=$p1
echo $p4
done < /tmp/tt.txt
exit

The output is this
BAKDIR = /home/mzullo/
$BAKDIR - MERRILL.*BTA* - 45
$BAKDIR
"$BAKDIR" - MERRILL.*BTA* - 45
"$BAKDIR"

The file
/tmp/tt.txt contains
$BAKDIR MERRILL.*BTA* 45
"$BAKDIR" MERRILL.*BTA* 45

How do I get UNIX to treat the value in $p1 “$BAKDIR” as a variable?

Last edited by mzullo; 05-21-2008 at 05:03 PM.. Reason: Add some comments up front
# 2  
Old 05-21-2008
You want eval

Code:
eval echo -n \${$p1}
echo " - $p2 - $p3"

I split it up in two separate echos because the quoting / backslashing becomes rather hideous if you want regular variables and eval in the same command.
# 3  
Old 05-21-2008
That did not work. It gave me "${$BAKDIR}: 0403-011 The specified substitution is not valid for this command"
# 4  
Old 05-22-2008
Oh yes, right, you need to modify that slightly.

Code:
eval echo -n $p1

I made it too complex because it's the usual answer, to a slightly more complex question. Sorry.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with passing variable

I have file called in in.txt contains with the below lines I want to display the lines between the value which I would be passing. one two three four five ten six seven eight Expected output if I have passed one and ten two three four five (8 Replies)
Discussion started by: mychbears
8 Replies

2. UNIX for Advanced & Expert Users

Passing variable as input & storing output in other variable

I have a below syntax its working fine... var12=$(ps -ef | grep apache | awk '{print $2,$4}') Im getting expected output as below: printf "%b\n" "${VAR12}" dell 123 dell 456 dell 457 Now I wrote a while loop.. the output of VAR12 should be passed as input parameters to while loop and results... (5 Replies)
Discussion started by: sam@sam
5 Replies

3. Shell Programming and Scripting

Passing dynamic variable within another variable.

I have a small program which needs to pass variable dynamically to form the name of a second variable whose value wil be passed on to a third variable. ***************** Program Start ****************** LOC1=/loc1 PAT1IN=/loc2 PAT2IN=/loc3 if ; then for fpattern in `cat... (5 Replies)
Discussion started by: Cyril Jos
5 Replies

4. Shell Programming and Scripting

Passing variable with *

Hi Folks, I would like to pass a variable with a wild card in an argument. My script works if I don't use a wildcard but fails when I use *. I want to use the script like: scriptname -F <filename*> @ i = 0 while ($i <= ${#argv}) switch ($argv) case -F: set j = `echo $i +1... (2 Replies)
Discussion started by: dixits
2 Replies

5. Shell Programming and Scripting

Passing variable and returning

1. I setup a website on html with a single text box and a submit button. 2. When the user enters the data into the text box and clicks submit - The output is displayed on a php site I setup as well *Clearly I don't know much of PHP or HTML* So now, I want to.... take this output, convert it... (1 Reply)
Discussion started by: svalenciatech
1 Replies

6. Shell Programming and Scripting

passing a variable inside another variable.

Any help would be great. I know this is a dumb way of doing this, but I would like to know if there is a solution doing it this way. I'm very new at this and I'd like to learn more. Thanks! :D:D count=0 while ; do echo "enter your name" read name_$count let count=count+1 done ... (2 Replies)
Discussion started by: reconflux
2 Replies

7. UNIX for Dummies Questions & Answers

passing a variable inside a variable to a function

I would like to know how to pass a variable inside a variable to a function. sample code below -------------- for x in 1 9 do check_null $C$x ##call function to check if the value is null if then echo "line number:$var_cnt,... (2 Replies)
Discussion started by: KingVikram
2 Replies

8. UNIX for Advanced & Expert Users

passing value to a variable in a SQL

Hi Folks, This is a small chunk of the bigger problem which i am facing and some help here will help me resolve the rest of the issue. Problem is that i need to pass the value of a variable from a shell script to a SQL query (infact a lot of SQL's) i have the following solution but somehow... (4 Replies)
Discussion started by: kamitsin
4 Replies

9. Shell Programming and Scripting

variable passing to sed

I m trying to pass variable to sed. export var=140920060731 sed -e '/$var/d' file but no luch so far..? any body has any idea abt it Is there any way to pass variable to SED? Thanks , Manish (2 Replies)
Discussion started by: Manish Jha
2 Replies

10. UNIX for Dummies Questions & Answers

Variable passing

Hi, If a script A(Parent) is running and script B(child) is run from script A, will the variables in script A be past to script B? Will the variables exist only for the duration of running the script? Thank you (2 Replies)
Discussion started by: whugo
2 Replies
Login or Register to Ask a Question