Exporting Value of a Variable as a Variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exporting Value of a Variable as a Variable
# 15  
Old 10-03-2008
yeah thanks.
i guessed the code will be like this.

Thanks again.
# 16  
Old 10-03-2008
I think i get it: you have a configuration file, which contains several declarations, all of the form IDENTIFIER=VALUE. You want these identifiers to become part of your environment and you to export them so that all forked processes inherit them.

The solution is way easier than you thought: first step is to get these declarations to your environment:

Code:
. /path/to/declaration_file

This will execute declaration_file in the current environment and since "IDENTIFIER=VALUE" is a perfectly legal statement, which creates/populates a variable your declarations will create the variables with the correct content. You can use the exportall capability of the Korn shell to have these variables exported automatically (see "man ksh" for the necessary flag, i have no ksh manual at hand as i write this). The complete code would look like:

Code:
set -<export_all_flag>
. /path/to/declaration_file
set +<export_all_flag>

If you want to do it all "by hand" as an alternative you can do the following: read in the declaration file, line by line, split each line at the "=" character to extract the identifier part and use a loop to set each variable and export it utilizing a generous dose of "eval":

Code:
cat declaration_file | while read chLine ; do
     $chLine
     eval export ${chLine%%=*}
done

I hope this helps.

bakunin

PS: On a sidenote and for the special edification of colleague era i'd like to expand on the "cat" above: i like my while-loops head-driven because it is easier to debug when they are longer. This is why i always write "cat file | while ...." instead of "while ... done < file".

A second reason is that in this case i would try to improve on the robustness of the declaration file format by making empty lines, comments, trailing/leading blanks and the like possible. In a real-world script of mine the passage would probably look like this:

Code:
sed 's/#.*$//
     s/[<spc><tab>]*=[<spc><tab>]*/=/
     s/^[<spc><tab>][<spc><tab>]*//
     s/[<spc><tab>][<spc><tab>]*$//
     /^$/d' file |\
while read chLine ; do
     ...etc.

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

2. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

3. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

4. Shell Programming and Scripting

Export command variable exporting problem

I have a txt file from which i am assiging a value to a variable using the code in script1 script1.sh export f=$(sed -n "/Freq *=/ s/.*= *//p" ${R_path}/output.txt) echo "$f" --------> this works in script2 ( which executes the script1) eval ./script1.sh if && ; then echo... (1 Reply)
Discussion started by: shashi792
1 Replies

5. Shell Programming and Scripting

exporting variable

Hi All; I m working on a script and came across an issue ,to explain it briefly here is the sample code echo $HOSTNAME|while read IN; do var=`echo $IN|awk -F "-" '{print $2}'`; export var; echo $var; done now I get the value of $var but when it is out of the while loop it does not return... (3 Replies)
Discussion started by: maverick_here
3 Replies

6. Shell Programming and Scripting

Exporting my dynamical variable won't work?

Even though the idea "might" not be great I still wrote this piece of code to get practice.. Which means that it is the CODE that matters here. Anyways; The intension is to create a program(or do we call it script?) that searches recursively through a folder to find a file - stored in a... (4 Replies)
Discussion started by: Pesk
4 Replies

7. Shell Programming and Scripting

How to define a variable with variable definition is stored in a variable?

Hi all, I have a variable say var1 (output from somewhere, which I can't change)which store something like this: echo $var1 name=fred age=25 address="123 abc" password=pass1234 how can I make the variable $name, $age, $address and $password contain the info? I mean do this in a... (1 Reply)
Discussion started by: freddy1228
1 Replies

8. Shell Programming and Scripting

Insert a line including Variable & Carriage Return / sed command as Variable

I want to instert Category:XXXXX into the 2. line something like this should work, but I have somewhere the wrong sytanx. something with the linebreak goes wrong: sed "2i\\${n}Category:$cat\n" Sample: Titel Blahh Blahh abllk sdhsd sjdhf Blahh Blah Blahh Blahh Should look like... (2 Replies)
Discussion started by: lowmaster
2 Replies

9. Solaris

variable exporting

Hi, can anyone tell me the difference between the below two examples: Eg-1: # name=bravo # echo $bravo what would be the o/p Eg-2: # name1=jhonny # export name1 # echo $name1 what would be the o/p If the o/p's of both examples are the same then what is the use of the cmd export... (3 Replies)
Discussion started by: rahul_11d
3 Replies

10. Shell Programming and Scripting

Exporting variable from config file

Hi, I am exporting the environment variable from config file, but when I echo the variable it does not display any value. Here is the snippet of the code #!/bin/sh export ENVIRONMENT_ROOT_DIRECTORY="/cb/$ENVIRONMENT" echo $ENVIRONMENT_ROOT_DIRECTORY ${JAVA_HOME}/bin/java... (2 Replies)
Discussion started by: bhavnabakshi
2 Replies
Login or Register to Ask a Question