Link to home
Start Free TrialLog in
Avatar of jculkincys
jculkincysFlag for United States of America

asked on

bash dynamic variable assignment

I need to assign values to dynamic variables

EX "$time"ofpurchase=$var

andy tips?

I keep getting "command not found error"

thanks
jculkincys
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

help eval
SOLUTION
Avatar of Duncan Roe
Duncan Roe
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jculkincys

ASKER

I almost got it

I just need a little more hand holding

I have dyname name on each side of the equation

eval "$time"_u_staff=$staff  # doesnt work

I am trying to get more info

does the above help at all?
in order to better separate your bash variables, the preferred method is

eval ${time}_u_staff=${staff}
Hmm I am stil getting errors

Portion of Code:

        eval ${time}_u_faculty=${faculty}
        echo "${time}_u_faculty --> $(${time}_u_faculty)"


Output of script with set -xv:

+ eval 00_u_faculty=0
00_u_faculty=0
++ 00_u_faculty=0
./analyzelogsdev.sh: 00_u_faculty=0: command not found
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the help

any tips on reading a dynamic variable?

for example while in a for loop I want to pull all the values back

Ex.

for time in 1 2 3
do
   echo ${u_faculty_${time}}

done

I am stuck with the following

+ time=01
echo `\${u_staff_${time}}`
${u_staff_${time}}
./analyzelogsdev.sh: ${u_staff_${time}}: bad substitution
++ echo
+ echo 'PLEASE WORK: '
PLEASE WORK:
+ exit 0
you cannot use embedded braces {}, use this syntax:

eval echo "\$u_faculty_${time}"