Sponsored Content
Full Discussion: Dynamic variables and eval
Top Forums Shell Programming and Scripting Dynamic variables and eval Post 302413970 by yonderboy on Sunday 18th of April 2010 09:46:53 PM
Old 04-18-2010
Dynamic variables and eval

Hi,

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:

Code:
 
for i in `echo ${testarray[*]}`
        do
                let c=c+1
                eval "first$c=$i"
                while :
                do
                        let n=n+1
                        echo "Please enter a value for second var as first has already been obtained from array:"                     
                        read second
                        echo "Please enter a value for third var"
                        read third
                        echo "Please enter a value for fourth var"
                        read fourth
                        eval "second$n=${second}
                        echo "Would you like to run the while loop again?"
                        while read input
                        do
                                if [[ $input == "y" || $input == "Y" ]];then
                                        eval "first$c\second$n=${second}${n}"
                                        eval "first$c\second$n\third=${third}"
                                        eval "first$c\second$n\fourth=${fourth}"
                                        break
                                elif [[ $input == "n" || $input == "N" ]];then
                                        break 2
                                else
                                        echo "Invalid response!"
                                fi
                        done
                done
        done


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
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamic variables within shell script

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)
Discussion started by: isingh786
2 Replies

2. UNIX for Dummies Questions & Answers

dynamic variables

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)
Discussion started by: max_payne1234
4 Replies

3. Programming

global variables and dynamic allocation

Hi, is it possible in C to allocate dynamically a global variable?? (3 Replies)
Discussion started by: littleboyblu
3 Replies

4. Shell Programming and Scripting

Problem with dynamic variables outside the loop

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)
Discussion started by: kk17
4 Replies

5. Shell Programming and Scripting

dynamic variables - eval - expand etc.

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)
Discussion started by: TehOne
5 Replies

6. Shell Programming and Scripting

Creating Dynamic Variables from a Flat File

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)
Discussion started by: sjrupp
2 Replies

7. Shell Programming and Scripting

Error in eval eval command to print html tags

anyone has any info on why this is complaining??? vivek@vivek-c5e55ef2e ~/TAC $ zoneCounter=1 vivek@vivek-c5e55ef2e ~/TAC $ optUsage1=23% vivek@vivek-c5e55ef2e ~/TAC $ eval eval echo "<th>Zone $zoneCounter </th><th align=\"left\"> \$optUsage$zoneCounter </th>" -bash: syntax error... (1 Reply)
Discussion started by: vivek d r
1 Replies

8. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

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)
Discussion started by: jeppe83
8 Replies

9. Shell Programming and Scripting

Generating dynamic variables

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)
Discussion started by: bhaskar v
3 Replies

10. UNIX for Beginners Questions & Answers

[BASH] eval command not expanding variables as expected.

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)
Discussion started by: ASGR
8 Replies
Devel::REPL::Profile(3pm)				User Contributed Perl Documentation				 Devel::REPL::Profile(3pm)

NAME
Devel::REPL::Profile - code to execute when re.pl starts SYNOPSIS
package Devel::REPL::Profile::MyProject; use Moose; use namespace::clean -except => [ 'meta' ]; with 'Devel::REPL::Profile'; sub apply_profile { my ($self, $repl) = @_; # do something here } 1; DESCRIPTION
For particular projects you might well end up running the same commands each time the REPL shell starts up - loading Perl modules, setting configuration, and so on. A mechanism called profiles exists to let you package and distribute these start-up scripts, as Perl modules. USAGE
Quite simply, follow the "SYNOPSIS" section above to create a boilerplate profile module. Within the "apply_profile" method, the $repl variable can be used to run any commands as the user would, within the context of their running "Devel::REPL" shell instance. For example, to load a module, you might have something like this: sub apply_profile { my ($self, $repl) = @_; $repl->eval('use Carp'); } As you can see, the "eval" method is used to run any code. The user won't see any output from that, and the code can "safely" die without destroying the REPL shell. The return value of "eval" will be the return value of the code you gave, or else if it died then a "Devel::REPL::Error" object is returned. If you want to load a "Devel::REPL" plugin, then use the following method: $repl->load_plugin('Timing'); The "load_plugin" and "eval" methods should cover most of what you would want to do before the user has access to the shell. Remember that plugin features are immediately available, so you can load for example the "LexEnv" plugin, and then declare "my" variables which the user will have access to. Selecting a Profile To run the shell with a particular profile, use the following command: system$ re.pl --profile MyProject Alternatively, you can set the environment variable "DEVEL_REPL_PROFILE" to MyProject. When the profile name is unqualified, as in the above example, the profile is assumed to be in the "Devel::REPL::Profile::" namespace. Otherwise if you pass something which contains the "::" character sequence, it will be loaded as-is. AUTHOR
Matt S Trout - mst (at) shadowcatsystems.co.uk (<http://www.shadowcatsystems.co.uk/>) LICENSE
This library is free software under the same terms as perl itself perl v5.14.2 2012-06-02 Devel::REPL::Profile(3pm)
All times are GMT -4. The time now is 03:35 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy