Assigning variables | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Assigning variables

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 02-05-2013
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
Assigning variables

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

Code:
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:

Code:
export RECIPIENT_MAIL_ID=${RECIPIENTS_${WMD_UPHASE4}}

error: bad substitution

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

Last edited by Scrutinizer; 02-05-2013 at 04:53 AM.. Reason: code tags
Sponsored Links
    #2  
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 6:34 PM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
Try
Code:
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..
Sponsored Links
    #3  
Old 02-05-2013
Arun Mishra Arun Mishra is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 11 June 2014, 4:30 AM EDT
Posts: 51
Thanks: 4
Thanked 0 Times in 0 Posts
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.
    #4  
Old 02-05-2013
Scrutinizer's Avatar
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 1 September 2014, 6:34 PM EDT
Location: Amsterdam
Posts: 9,386
Thanks: 273
Thanked 2,349 Times in 2,108 Posts
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..

Last edited by Scrutinizer; 02-05-2013 at 06:02 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
Arun Mishra (02-05-2013)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Help with reading and assigning variables wisdom Shell Programming and Scripting 6 03-17-2011 06:11 AM
Assigning variables using awk posix UNIX for Dummies Questions & Answers 2 05-06-2010 12:39 AM
variables not assigning in a function mac4rfree Shell Programming and Scripting 3 07-21-2009 10:41 AM
assigning variables to their defaults clx UNIX for Advanced & Expert Users 1 04-15-2009 10:22 PM
assigning variables k@ssidy UNIX for Dummies Questions & Answers 9 06-16-2005 10:20 AM



All times are GMT -4. The time now is 09:54 PM.