Dynamically setting of environment variable... Can it be done?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Dynamically setting of environment variable... Can it be done?
# 1  
Old 09-14-2011
Dynamically setting of environment variable... Can it be done?

Hi all,
I am fairly new to unix scripting and will like to know how to dynamically set the name of an environment variable to be used.

We have a .env file where we defined the names and locations of data files, trigger files, directories .... etc
Example of variables defined in .env
data_file_123=sales.dat
data_file_456=expenses.dat
data_file_789=profits.dat
I need to create a script (.ksh) that is flexible enough to load different data files depending on the parameter received. Also, based on this parameter I will need to verify if the files exist and this is where I will need to utilized the variables defined in the .env file.
The load script will received the parameter “123” or “456” or “789” (param1)

A couple of scenarios I tried that didn't work are:
if [[ ! -f $data_file_$param1 ]]; then .....

if [[ ! -f ${data_file_$param1} ]]; then .....
I hope I explained my issue adequately and that I've given enough information.
Thank you!
# 2  
Old 09-14-2011
If you have a new enough ksh, you can do things like this:

Code:
$ NAME="VAR"
$ VAR="1234"
echo "${!NAME}"
1234
$

If you don't, it's going to be ugly:

Code:
$ data_file_123=sales.dat
$ PARAM="123"
$ VARSTR="\${data_file_${PARAM}}"
$ echo "${VARSTR}"
${data_file_123}
$ eval echo "$VARSTR"
sales.dat

Beware, eval carries a hidden danger:

Code:
$ PARAM="} ; touch / ; \${1"
$ VARSTR="\${data_file_${PARAM}}"
$ echo "${VARSTR}"
${data_file_} ; touch / ; ${1}
$ eval echo ${VARSTR}

touch: setting times of `/': Permission denied
$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 09-14-2011
Corona688,

Thank you very much for replying so quickly to my post. Could you please kindly explain the first example you gave me, because I do not understand how to apply it and will like to try it.

Code:
$ NAME="VAR"
$ VAR="1234"
echo "${!NAME}"
1234
$

# 4  
Old 09-15-2011
Something more like your code:

Code:
data_file_123="whatever"
PARAM="123"
VAR="data_file_${PARAM}"
[ -f "${!VAR}" ] && echo "The file ${!VAR} exists"

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 09-15-2011
Corona688,

Once again thank you ! I got it to work.

environment variables
Code:
data_file_123=sales.dat
data_directory=inbound

In the script
Code:
 
param1=123
file_name=data_file_$param1
eval v_file_name='$'$file_name
 
if [[ ! -f $data_directory/$v_file_name ]]; then 
.....
.....

THANKS FOR YOUR HELP!!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Dynamically changing environment variable

Linux Redhat, BASH Shell. I want to put this in my .bash_profile I have log files that go to directory paths based in part on other variables such as went DB Name is set in memory. So if the DB Name changes the path to the log file changes. How do I create an environment variable I put into... (6 Replies)
Discussion started by: guessingo
6 Replies

2. UNIX for Dummies Questions & Answers

STTY Columns Setting Environment Variable?

I am wondering about the following: stty columns 140 I have found that a number of times I need to set my display columns to a high number (such as 140) but I have to do this every time I login to use putty/ssh. Can we set this with an environmental variable so that it is permanent? Also... (4 Replies)
Discussion started by: newbie2010
4 Replies

3. UNIX for Dummies Questions & Answers

Setting environment variable problem in Ubuntu?

I am trying to install timbl- memory based learner tools in ubuntu. it after unpacking the tar file it brings the following msg No package 'ticcutils' found Consider adjusting the PKG_CONFIG_PATH environment variable if you installed software in a non-standard prefix. Alternatively, you... (1 Reply)
Discussion started by: gbdaw
1 Replies

4. UNIX for Dummies Questions & Answers

setting a environment variable on linux

I want to set a enviroment variable VDC_DIR to a particular directory. I am doing it as export VDC_DIR=/abc it gets set but when i logout and do relogin than its not there. one way could be setting it in .profile file. but i have seen it on another box where it is not present in... (2 Replies)
Discussion started by: Jcpratap
2 Replies

5. Shell Programming and Scripting

problem with setting environment variable

shell script: #!/bin/csh set VAR=12345 echo $VAR will peacefully give the output 12345 at shell. I need to use C++ to do the same in some part of the code: string str = "12345"; retValue="set var1= "+str; system(retValue1.c_str()); system("echo $var1"); This doesn't create a system... (1 Reply)
Discussion started by: harshvardhan360
1 Replies

6. Shell Programming and Scripting

Setting Environment variable from value in file

I've searched Google and now this forum. Best guess is my search fu is not good (and it probably isn't). The Google search did bring me here. Background I have a number of Korn Shell scripts who all use one of 3 values for an environment variable used in the backup system. On occasion one or... (8 Replies)
Discussion started by: WolfBrother
8 Replies

7. Programming

Setting Environment variable..!

Hi, I already have one CPP program which invokes the C program.And the C program contains whole function definitions..!This is a working program..I have to enable the logs in both CPP as well as in the C program ..!So I am reading the enviornmental variable log path from the CPP and doing the... (2 Replies)
Discussion started by: Kattoor
2 Replies

8. UNIX for Dummies Questions & Answers

setting environment variable in awk

Dear all, I have a data sample... Dose: Summed ROI: Bladder ************************** Bin Dose Volume 001 0.700 100.000 002 0.715 99.998 168 3.142 0.368 169 3.157 0.338 170 3.171 0.292 Dose: Summed ROI:... (2 Replies)
Discussion started by: tintin72
2 Replies

9. Programming

Setting environment variable using JNI call

I have function declaration in Java and same function definition written in C programming language.. A JNI call from Java is made to a fuction...Function would set the environment variable { putenv(cEnvString1);} using C-built -in function ..and later return the encrypted string... putenv is... (6 Replies)
Discussion started by: shafi2all
6 Replies

10. Programming

Setting Environment Variable date

Hi to all... I'm currently running a C++ program in Unix environment and it is dependent to a Unix environment variable with a date value. ex: echo $DateToday 20060403 I want to change that date in my C++ program, changing the value date to 20061120 and revert back to original... (6 Replies)
Discussion started by: d3ck_tm
6 Replies
Login or Register to Ask a Question