Passing dynamic variable within another variable.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing dynamic variable within another variable.
# 1  
Old 12-30-2013
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 ******************

Code:
LOC1=/loc1
PAT1IN=/loc2
PAT2IN=/loc3
if [ -f $LOC1/* ]; then
for fpattern in `cat $script/filepattern.txt`
do 
for ffilename in `find $LOC1/ -type f`
do
filename=`echo $ffilename | cut -d "/" -f 3`
file=`echo $filename | cut -d "_" -f 1`
case "$file" in
$fpattern)
INLOC=${${file}IN}
echo $INLOC
echo $filename
;;
esac
done
done 
else
echo "Exiting Job as no files present."
exit
fi

***************** Program End ******************

List of files present in the /loc1 are as below
Code:
/loc1/PAT1_test.txt
/loc1/PAT2_test1.txt

content of filepattern.txt are as below

Code:
PAT1
PAT2

when i execute the program, i keep getting this error

Code:
INLOC=${${file}IN}: bad substitution

Please let me know if i am doing something wrong. or is there an alternate way to achieve this without having to use to much variables.

expected output is:
Code:
/loc2
PAT1_test.txt
/loc3
PAT2_test1.txt


Last edited by bartus11; 12-30-2013 at 11:50 AM.. Reason: Please use code tags
# 2  
Old 12-30-2013
What is this INLOC=${${file}IN} ???

I am not seeing any point of this line Smilie
# 3  
Old 12-30-2013
Code:
INLOC=${${file}IN}

will become a common variable within a seperate function repeteadly based on the different values of ${file}. The main issue i am facing here is that i am not able to set a value for INLOC.
# 4  
Old 12-30-2013
Shells do not usually support this programming style, and you should be very clear in the first place about what you want to achieve. You could consider using associative arrays available in recent shells, or using bash's indirect expansion (c.f. man bash).
As a very last resort, you could try to eval that expression, but you should be aware that using eval can be dangerous!
This User Gave Thanks to RudiC For This Post:
# 5  
Old 12-30-2013
Try:
Code:
INLOC="${file}IN"

This User Gave Thanks to bartus11 For This Post:
# 6  
Old 12-31-2013
Passing dynamic variable within another variable.

Thank you all for your valuable help.

RudiC, Took your advice modified the line to
Code:
eval INLOC='$'{${file}IN}

and it worked beautifully.

I have got the desired output from the program now.SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variable name in bash

Hello, I'm trying to build a small script to store a command into a dynamic variable, and I have trouble assigning the variable. #!/bin/bash declare -a var1array=("value1" "value2" "value3") var1arraylength=${#var1array} for (( i=1; i<${var1arraylength}+1; i++ )); do mkdir... (7 Replies)
Discussion started by: alex2005
7 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. UNIX for Dummies Questions & Answers

Dynamic Variable creation

I am trying to create some variables based on the input by the user, say if user entered 3 then 3 variables and if 5 then 5 variables. I am using a for loop for (( i=1; i <= $num; i++ )) do x="num" x+=$i done When i am using echo $x it will show num1 but now how to create variables... (3 Replies)
Discussion started by: Raj999
3 Replies

4. Shell Programming and Scripting

Dynamic file name in variable

Hi guys. i have a requirment as below. I have a scripts which perform for loop for i in /backup/logs -- it will give all the logs file SC_RIO_RWVM_20120413064217303.LOG SC_RIO_RWXM_20120413064225493.LOG SC_RIO_RXXM_20120413064233273.LOG ... do open script.sh ---- in this file... (3 Replies)
Discussion started by: guddu_12
3 Replies

5. Shell Programming and Scripting

Help with Dynamic variable

I need some variable help TEMP1=Jane TEMP2=Sue X=1 eval USER=TEMP${X} echo $USER This gives output USER1 I would like to get Jane I have tried eval USER='TEMP${X}' eval USER="TEMP${X}" eval USER=`TEMP${X}` (3 Replies)
Discussion started by: Jotne
3 Replies

6. Shell Programming and Scripting

Dynamic variable assignment

Hi all, I’m very new to UNIX programming. I have a question on dynamic variable 1. I’m having delimited file (only one row). First of all, I want to count number of columns based on delimiter. Then I want to create number of variables equal to number of fields. Say number of... (5 Replies)
Discussion started by: mkarthykeyan
5 Replies

7. Shell Programming and Scripting

dynamic variable name

I found one post in another site with a solution for my problem the below solution should explain what I want. #!/bin/sh first="one" second="two" third="three" myvar="first" echo ${!myvar} But this gives error 'bad substitution' System info SunOS sundev2 5.9... (3 Replies)
Discussion started by: johnbach
3 Replies

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

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

10. Shell Programming and Scripting

Dynamic Variable Declatation

Evening all, Have been trying to create the following environment variable: ${MD_SYSTEM}_ZZ_EMAIL_SUPPORT="myname@domain.com" However when the script that contains the above is executed it returns: ksh: MDQA_ZZ_EMAIL_SUPPORT=myname@domain.com: not found Is what I'm trying to do... (2 Replies)
Discussion started by: Cameron
2 Replies
Login or Register to Ask a Question