Setting environment variable using shell script

Hi All,

I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to access by the menu script to login and proceed with the functions.

I tried to execute the config file from the shell script, but the environment variables are dying after the menu script is exited. The question is how to set those environment variable in the terminal for the complete session even after exiting the script, because if the menu gets failed in the 2nd or 3rd step and when the user tries to execute the script again it is mandating the user to run the config file inorder to set the env variables which is a pain for the user.

Please give your suggestions !!!

Thanks,

You could try source'ing your config

$ source config.file

Or

$ . config.file

If my understanding is right, the source command is used to use the external file for the menu script which it is being called. But my query is once I execute the config file the env variables should be set not only to the child process which is created but to the parent process where the menu script is executed.

please let me know if you couldn't catch my point.

Thanks for your help

Arun
Newly set env variables are not available for the parent shell. However, if you need, what you can do is:

> Use 'printenv' command and redirect the output to a file from your function.
> Run this file once you are back in the main menu either using . or source depending on your shell.

Guru.

Even though I run while returning back to the main menu it doesn't help me because once the script exits the env variables would die, so there is no way I can set the env variable to the parent process ??

Arun
This is what i meant:

File f2:

#!/usr/bin/ksh

echo in file f2
export X="welcome"
printenv |grep -v printenv  | sed 's/^/export /' > f3

File f1:

#!/usr/bin/ksh

echo in main f1
f2
. f3
echo X is $X

On running the file f1:

in main f1
in file f2
X is welcome

X is set in the child shell f2, and we got it back in the parent f1.

Guru.

Guru,

Thanks for the explanation.