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 [ "$(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:

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!

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

thanks alot, that works like a harm :slight_smile:

Hello,

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

Example:

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:

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!!!

Just a logic problem - you were assigning VAR2 on 3rd loop:

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:

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

Thanks!