Global variables not taking effect when set via called script.
Red HatThis forum is for the discussion of Red Hat Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Global variables not taking effect when set via called script.
Hi there,
I have a script which lists all the SID listed in /etc/oratab and then after the user select which SID to set, it calls another script to set the SID and other environment variables.
The script runs just fine and does all that is expected, but the variables are not set permanently, can't figure out why. Everything works fine when I run the setsid.sh out of this script. Here is the script:
#!/bin/bash
#set -x
#clear
echo ""
echo ""
let i=0
ORATAB=/etc/oratab
. /usr/bin/showsid.sh
cat $ORATAB | while read LINE
do
case $LINE in
\#*) ;;
\**) ;;
"") ;;
*)
let i=i+1
SID=`echo $LINE | awk -F ":" '{print $1}'`
echo "$i. => $SID"
;;
esac
done
echo "Please select the SID you want to set or Hit Enter to Exit:";
read i;
if [ x$i == 'x' ]; then
echo 'Nothing changed. Thanks You...';
exit 1;
fi
let j=0
cat $ORATAB | while read LINE
do
case $LINE in
\#*) ;;
\**) ;;
"") ;;
*)
let j=j+1
SID=`echo $LINE | awk -F ":" '{print $1}'`;
if [ $i == $j ]; then
echo "This is the SID: $SID"
/usr/bin/setsid.sh $SID;
exit 1;
fi
;;
esac
done
It's simply impossible to use a script to change the environment of it's parent shell. Changes can never propagate "upwards", they can only affect the process they are executed from and whatever sub-processes are spawned from it. This means that once your script exits, all variables set within it are lost, and there's nothing you can do about it.
The only way to go about it is to run it as a command that executes within the top-level shell, or to set it through a sourced file, such as one of your start-up scripts.
For example, you could convert your script into a shell function and add it to your bashrc (or to source it when needed from another file). Then it should work as expected.
A related idea would be to have your script output to a temporary file that contains the settings you want, then you'd only need a small function, or even just an alias, in your shell to import the settings from that file.
PS: Please use [code][/code] tags around your code, to preserve formatting and to improve readability.
Last edited by David the H.; 08-03-2010 at 03:06 PM.
David,
But, as I mentioned if I run $./setsid.sh $SID it works just fine. I set all the environment variables in this script with out sourcing it from .profile??
David,
But, as I mentioned if I run $./setsid.sh $SID it works just fine.
What David wrote is correct so it is hard to understand how it can work as you describe. Can you run setsid.sh as you describe, then run the commands that show it has worked "just fine" then copy and paste the command prompt session into this thread?
I admit I'm a little confused as to what exactly is going on here. Is that the actual output of the setsid and showsid commands? After both scripts exit, do any environmental variables appear in the shell? In other words can you "echo $ORACLE_SID" and get the same answer? I'll bet not.
Could you explain in a little more detail what the purpose of all these commands is? I think it would be useful to see the code of setsid.sh as well, if possible.
David,
Purpose of these command is basically to set the ORALCE environment. setsid is an alias to a script called setsid.sh and showsid is an alias to showsid.sh
setsid <SIDname> will set the oracle environment to that SID and showsid shows what the current SID is set to, and the other related env variables.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.