Assigning variables

i have variables RECIPIENTS_DEVL,RECIPIENTS_UACC,RECIPIENTS_PROD
i have a case statement to get the phase variable:

case ${WMD_UPHASE1} in
   u) WMD_UPHASE4=UACC;;
   i) WMD_UPHASE4=DEVL;;
   p) WMD_UPHASE4=PROD;;
   d) WMD_UPHASE4=DEVL;;
   *) WMD_UPHASE4=DEVL;;
esac

I am unable to assign like the below:

export RECIPIENT_MAIL_ID=${RECIPIENTS_${WMD_UPHASE4}}

error: bad substitution

Can you let me know what i am doing wrong here?

Try

eval export RECIPIENT_MAIL_ID=\${RECIPIENTS_${WMD_UPHASE4}}

eval is potential security risk, you need to control the $WMD_UPHASE4 variable and not read it from user input or an input file that others can change..

thanks for the reply,

Can you explain what eval does here? and why you put \?
why is it a security risk? and if there is any other way you suggest as i need to have this variable from input only.

eval is a two-stage operation, it first concatenates the arguments with spaces in between and then executes the resulting command. The backslash is there to keep the first $-sign from being evaluated before the concatenation phase. The backslash gets removed before the second phase.

If a user is able to control what is in variable $WMD_UPHASE4 then he could have his code executed with the permissions of the user executing the script.

A better approach would be the use of arrays. In ksh93 and bash4 that can be associative arrays, which means you could use words as the array index instead of only numbers..

1 Like