I need some direction with the following. The below code is semi-psuedo code which will hopefully make it easier to understand what I am trying to achieve:
First of all I am looping through an array ( let's call it ${testarray}) with a "for" loop and assigning the value of that element of the array for that iteration to the ubiquitous ${i}. I am also setting up a loopcount variable called ${c} which keeps track of the number of times the "for" loop has been called.
The first stage of the for loop simply assigns the value of ${i} to a more meaningful name (${first}${c}).
I then start a nested while loop with yet another counter called ${n}which, (while true) reads in another variable called "${second}", another called "${third}" and yet another called ${fourth}.
I then use eval to give ${second} a unique name (depending on the iteration. (e.g. for the first iteration, ${second} would be renamed ${second1}, for the second iteration, ${second2}, then ${second3} etc..). ${third} and ${fourth} do not need to be unique for the iteration.
Then, depending on user input I want to EITHER start again at the top of the while loop (thereby keeping the iterative value for ${first}${c}) , but increment the count value of ${second};
OR start the "for" loop again and increment the value of ${first}${c}, followed once again by the other values.
What I hope to end up with is a bunch of dynamic variables of the form (as an example) ${first1second1third} and ${first1second1fourth) or ${first1second2third} etc....
What I am having trouble with is getting the dynamic variables to be named correctly and evaluated by the shell correctly.
Hope this makes sense.....
Thanks in advance
Last edited by yonderboy; 04-19-2010 at 12:48 AM..
Reason: clarity
Thanks for your response. I think perhaps I have been too verbose in my explanation. What it boils down to is this.....
If I have the following variables set up for an iteration:
c=1
n=2
sl1=10
Is it possible (with eval or any other tool for that matter) to assign the value of sl1 to an (as yet undefined) variable called $di1sl2.
This would be easy enough if the name of $sl1 and the undefined variable did not change for every iteration.
So the way I see it happening (in psuedo-code) would be:
Then when you:
You should get:
10
Hopefully you can now see my issue, namely that the variable that contains "10" will be differently named for each iteration (it could be $sl1, $sl2 $sl3 etc). so this means that eval will need to deal with a "variable within a variable" - notice the two pairs of curly braces in the same variable in the example above.
Does this make it any clearer?
Many thanks....
Last edited by yonderboy; 04-19-2010 at 10:39 PM..
Hi Guys,
I wrote a collection of bash functions years ago and now need to use them again but
I'm getting some error messages when eval tries to expand the variables names.
I recollect that I used the shopt command to set one of the options but I can't quite
remember the command that I... (8 Replies)
Hi ,
i am unable to generate dynamic variables can any one please help me on the below issue
j=1
{record_count_"$j"}=`db2 -xselect substr\(job_name,24\) rec_count from $libname.audit_table_nrt where job_name like \'DATAMART_DEL_RUN%\' and STS_FLAG=\'E\' and seq_no=$i`
echo " record... (3 Replies)
Hi all,
I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea.
n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done
The error message is:
bash: p1=line1: command not found
bash: p2=line2: command... (8 Replies)
Greetings all,
Been trying to do my Googling and forum searches but can't seem to lock in on a solution.
I have a script that parses a log and collects all the uniq events to a flat file. Some days might have 50 unique events, other days might have 75. (Hence my reference to dynamic.)
... (2 Replies)
Hello,
so i'm making a script, using dynamic variables and trying to expand them. So far it hasn't worked out too well so it seems that I need some help from you, the elite.
Example:
#!/bin/sh
counter=0
until (($counter>5))
counter2=1
until (($counter2>6)); do
if ;... (5 Replies)
Hi All,
Shell is ksh
I've given portion of the script here to explain the problem.
It will accept 2 input parameters .
in_file1=$1
in_file2=$2
outbound_dir=/home/outbound
for i in 1 2
do
eval file$i=$outbound_dir/\$in_file$i
eval echo "filename is \$file$i"
... (4 Replies)
I am new to unix and the following problem is bugging me.:confused:
var1="hello1"
var2="hello2"
var3="hello3"
counter=1
while
do
echo $var$counter
done
the idea here is to display the value of "var" based on the counter.
I am using the korn shell. I used array here but the... (4 Replies)
Hi Gurus,
I have a requirement of writting the shell script where it should ask me two values
FND_TOP=/d02/app/oracle/xxx/fnd/11.5.0
CDCRM_TOP=/d02/app/oracle/xxx/cdcrm/11.5.0
and then keep these values stored as variables for the execution of rest of the script.
Because, I have to... (2 Replies)