Visit The New, Modern Unix Linux Community


dynamic variables - eval - expand etc.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting dynamic variables - eval - expand etc.
# 1  
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:

Code:
#!/bin/sh

counter=0
until (($counter>5))
    counter2=1
    until (($counter2>6)); do
      if [ "$(eval echo \$testing$counter)" ]; then
        eval testing$counter="$(eval echo \$testing$counter) $counter2"
      else
        eval testing$counter="$counter2"
      fi
      let counter2=counter2+1
    done
    let counter=counter+1
done

So I would expect the following results:

Code:
testing1="1 2 3 4 5"
testing2="1 2 3 4 5"
testing3="1 2 3 4 5"
testing4="1 2 3 4 5"
testing5="1 2 3 4 5"

however it will fail on line 8, so how am I suppose to do this?

Thanks for all the help in advance!
# 2  
Code:
for counter in 1 2 3 4 5; do
  for counter2 in 1 2 3 4 5; do
      if [ $counter2 = 1 ]; then
        eval testing$counter=$counter2
      else
        eval testing$counter=\"\$testing$counter $counter2\"
      fi
  done
done

# 3  
thanks alot, that works like a harm Smilie
# 4  
Hello,

how do I check whenever a dynamic var exists and how can I compare them in a if statement?

Example:

Code:
counter=0
example=1
until (($counter>2)); do
      if [ ! "$(eval echo "\$testing$example")" ]; then
            echo "FIRST LOOP, CREATING DYN VAR1"
            eval testing$example="test"
      else
            echo "SECOND LOOP, CREATING DYN VAR2"
            eval testing2$example="test"
            echo "SECOND LOOP COMPARING DYN VAR1 to DYN VAR2"
            if [ "$(eval echo \$testing$example)" == "$(eval echo \$testing2$example)" ]; then
                echo "SECOND LOOP MATCH!!!"
                echo "SECOND LOOP CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP"
                eval testing2$example="test2"
            else
                echo "THIRD LOOP NO MATCH!!!"
            fi
      fi
      let counter=counter+1
done

Expected result:

Code:
FIRST LOOP, CREATING DYN VAR1
SECOND LOOP, CREATING DYN VAR2
SECOND LOOP MATCH!!!
SECOND LOOP CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP
THIRD LOOP NO MATCH!!!


Last edited by TehOne; 03-25-2011 at 04:10 PM..
# 5  
Just a logic problem - you were assigning VAR2 on 3rd loop:

Code:
example=1
for counter in 0 1 2; do
      if [ ! "$(eval echo "\$testing$example")" ]; then
            echo "FIRST LOOP, CREATING DYN VAR1"
            eval testing$example="test"
      else
         if [ ! "$(eval echo "\$testing2$example")" ]; then
              echo "SECOND LOOP, CREATING DYN VAR2"
              eval testing2$example="test"
         fi
            echo "COMPARING DYN VAR1 to DYN VAR2"
            if [ "$(eval echo \$testing$example)" == "$(eval echo \$testing2$example)" ]; then
                echo "MATCH"
                echo "CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP"
                eval testing2$example="test2"
            else
                echo "NO MATCH"
            fi
      fi
done

If your doing a lot of this sort of stuff why not write notdefined and dynval functions:

Code:
notdefined() {
[ ${!1-X} != ${!1-Y} ]
}
dynval() {
  printf ${!1}
}
example=1
for counter in 0 1 2; do
      if notdefined testing$example ; then
            echo "FIRST LOOP, CREATING DYN VAR1"
            eval testing$example="test"
      else
            if notdefined testing2$example ; then
              echo "SECOND LOOP, CREATING DYN VAR2"
              eval testing2$example="test"
            fi
            echo "COMPARING DYN VAR1 to DYN VAR2"
            if [ "$(dynval testing$example)" == "$(dynval testing2$example)" ]; then
                echo "MATCH"
                echo "CHANGING SECOND DYN VAR TO GET NO MATCH NEXT LOOP"
                eval testing2$example="test2"
            else
                echo "NO MATCH"
            fi
      fi
done


Last edited by Chubler_XL; 03-25-2011 at 04:28 PM..
# 6  
Thanks!

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #172
Difficulty: Easy
Red Hat is the largest contributor to the GNOME desktop environment.
True or False?

10 More Discussions You Might Find Interesting

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

2. UNIX for Beginners Questions & Answers

Expand Variables and Wildcards into another variable.

Dear Forum members, I am having trouble getting the complete filename (and directory path) in a variable. Output directory mentioned in the code have three files: DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_london.out DISPLAY_CITY_DETAILS_15-05-2019-08-29-26_MIGRATE_paris.out... (4 Replies)
Discussion started by: chetanojha
4 Replies

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

4. Shell Programming and Scripting

For loop using input file doesn't expand variables

Hi, I'm using a for loop reading from an input file that contains files, whose path includes a variable name. But the for loop doesn't expand the variable and therefore can't find the file. Here's an example: File BACKUPFILES /home/John/alpha /home/Sue/beta... (8 Replies)
Discussion started by: Hesiod
8 Replies

5. Shell Programming and Scripting

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: for i in `echo ${testarray }` do let c=c+1 eval "first$c=$i" while... (4 Replies)
Discussion started by: yonderboy
4 Replies

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

7. UNIX for Dummies Questions & Answers

how to expand environment variables in a file?

I am new to unix and would appreciate if someone could help. I have an environment variable SourceFilePath=/db1/Src/test set on the unix server. I want to expand this SHELL variable in a file using any command sed, awk etc File contents is as follows: var=$SourceFilePath/file.txt ... (2 Replies)
Discussion started by: debbie15
2 Replies

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

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

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

Featured Tech Videos