How to get the value of a variable which is having another value in environmental script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to get the value of a variable which is having another value in environmental script?
# 1  
Old 07-23-2010
MySQL How to get the value of a variable which is having another value in environmental script?

Dear Folks,
I am facing an issue in getting a value of a variable.Let me explain the scenario.

I am having a list file say files.list in which I have mentioned
Code:
1  FILE1
          2  FILE2

Then I am having an set_env.ksh in which I mentioned
Code:
FILE1=/clocal/data/user/userdata.txt
FILE2=/clocal/data/scripts/script.ksh
export FILE1 FILE2

Then I am having a main script in which I want to accesses the value of FILE1 by giving "files.list" as input. I am doing something like this


Code:
#!/usr/bin/ksh
.set_ki_env.ksh
 
##Function##
getFileName(){
FileName=$1;
echo $FileName
##This is printing the value from list file which is FILE1
}
 
##End###
 
 
while read num filename
do
getFileName $filename
done < files.list

I am getting the output as FILE1. I want to get the value of FILE1 from the set_env.ksh which is /clocal/data/user/userdata.txt.

I have tried $("$FileName"); But not getting the required output.

Please help me with this.
Thanks in advance
# 2  
Old 07-23-2010
You have to "source" the file with the variables like:
Code:
. ./set_ki_env.ksh

To get the value of the variable you can use the eval command like:
Code:
#!/usr/bin/ksh

. ./set_ki_env.ksh

while read num filename
do
  eval "file=\$$filename"
  echo $file
  ...
  ...
done < files.list

# 3  
Old 07-23-2010
Thanks..Working

Code:
eval "file=\$$filename"

This is the absolute answer for my question. Thanks a lot..It is workingSmilie
# 4  
Old 07-23-2010
Quote:
Originally Posted by dinesh1985
Code:
eval "file=\$$filename"

This is the absolute answer for my question. Thanks a lot..It is workingSmilie
What does the \$$filename do? I don't think I have ever seen this before.
# 5  
Old 07-23-2010
Quote:
Originally Posted by TFord
What does the \$$filename do? I don't think I have ever seen this before.
eval is used here to obtain the value of a variable whose name is derived from the value of another variable.

Suppose you have an environment variable FILE1 with the content as in the scenario above:
Code:
FILE1="/clocal/data/user/userdata.txt"

If you assign the name of the environment variable to the variable like:
Code:
filename="FILE1"

filename doesn't have the value "/clocal/data/user/userdata.txt" but just "FILE1".

With the command:
Code:
eval "file=\$$filename"

eval expands $filename to FILE1, then the shell expands $FILE1 (hence the escaped $) and assigns the content to the variable $file.

Now the variable $file contains "/clocal/data/user/userdata.txt".
This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Issue with cron and environmental variable

My shell script it.sh.I am calling bip.sh from it.sh #!/bin/sh ORACLE_HOME=/var/opt/oracle/product/10g; export ORACLE_HOME PATH=$PATH:$ORACLE_HOME/bin:/bin:/usr/bin; export PATH today=`date "+%m-%d-%Y %H:%M:%S"`; export today CUR_DIR=$1; export CUR_DIR LOG_FILE=$CUR_DIR/error.log;... (4 Replies)
Discussion started by: rafa_fed2
4 Replies

2. Shell Programming and Scripting

Clearing a environmental variable

i set a variable from the command line: export GANG="james,roy,martin" i can access this variable ($GANG) from a script. but each time i run the script, the variable keeps getting bigger. more info keeps getting added to it. Is there anyway i can make the $GANG variable contain the... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Advanced & Expert Users

Environmental variable

i want to set environmental variables in solaris and redhat. it should show the current directory and the default shell should be bourne shell. along with it should show the hostname.. like this hostname{/home/vipin/data}# ifconfig Thanks in advanced.:wall: Please use code tags.... (1 Reply)
Discussion started by: vipinkumarr89
1 Replies

4. OS X (Apple)

.local hostname available as an environmental variable?

Hola - I've got a script for logon which populates a database with some info when a user logs on so that we can tell how many public terminals are in use & show users which ones are free. However, the machines are DHCPed and so using $HOSTNAME gives me an unstable name for them since of course... (3 Replies)
Discussion started by: gentinphilly
3 Replies

5. Shell Programming and Scripting

Setting Environmental Variable

I have a request from a programmer to set an env variable for him. I know how to do it for bash shell by adding the following line to .bash_profile export $VAR=/home/code/project/ But this will be applicable only when he is in his bash shell. What is the procedure to be followed to make... (2 Replies)
Discussion started by: Tuxidow
2 Replies

6. UNIX for Dummies Questions & Answers

SQL loader script - ORACLE environmental variable

I am new in unix.. I am running a sql loader script where I have to specify the data file path but the file name contains spaces in it so giving error multiple arguments I have tried it with "" and '' but does n't work the command is : $ORACLE_HOME/bin/sqlldr... (1 Reply)
Discussion started by: Sandip Dey
1 Replies

7. UNIX for Dummies Questions & Answers

How does the PATH environmental variable work?

Hello. I have a question about how the PATH environment variable works. I wrote a script in $HOME/bin/gvim. I want it to be called instead of /usr/bin/gvim, so I've placed it before in the PATH. However, it is still the old one that is found. If I open an other terminal, I have the... (6 Replies)
Discussion started by: qwer
6 Replies

8. Shell Programming and Scripting

setting ksh environmental variable

Hi, I have problem setting up environmental variables. The idea is to start with main.ksh script that will run setting.ksh, and in side of it I'll set up variables. Please take a look at my code, and help me to find my mistake. Thanks, Mila Main.ksh look like this: #!/usr/bin/ksh #... (2 Replies)
Discussion started by: mefquik
2 Replies

9. Shell Programming and Scripting

Environmental Variable

Hi, I'm exporting an environmental variable from a C program using putenv function. I'm calling the exe of the C program from shell script. But when I display the environmental variables from the Shell script, My varaible is not getting displayed. Can anyone please tell me how to get it in... (2 Replies)
Discussion started by: janemary.a
2 Replies

10. Shell Programming and Scripting

Set Oracle Environmental Variable !!

Can someone send me a shell script to set all Oracle environment variable which is working. I have the following script which works but not 100%. Please advice what you think is wrong. if # Command executed from a terminal then ORACLE_SID="" ... (4 Replies)
Discussion started by: uuser
4 Replies
Login or Register to Ask a Question