LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat
User Name
Password
Red Hat This forum is for the discussion of Red Hat Linux.

Notices


Reply
  Search this Thread
Old 08-03-2010, 02:30 PM   #1
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Rep: Reputation: 0
Unhappy 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


Help is appreciated..


Thanks

Nirmal
 
Old 08-03-2010, 02:57 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 08-03-2010, 03:32 PM   #3
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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??


Thanks

Nirmal
 
Old 08-03-2010, 03:40 PM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by nsingh49 View Post
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?
 
Old 08-03-2010, 03:46 PM   #5
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Here we go!


oracle@[/u01/app/oracle] showsid


ORACLE_SID = inftst1
ORACLE_HOME = /u01/app/oracle/product/10.2.0.4/db
PATH = /usr/kerberos/bin:.:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.:/usr/sbin:/u01/app/oracle/product/10.2.0.4/db/bin:/usr/openwin/bin:/u01/app/oracle/utils:/u01/app/oracle/product/10.2.0.4/db/rdbms/admin:/u01/app/oracle/product/10.2.0.4/db/network/admin:/u01/app/oracle/product/10.2.0.4/db/OPatch
ORACLE_BASE = /u01/app/oracle
ORA_NLS33 = /u01/app/oracle/product/10.2.0.4/db/ocommon/nls/admin/data
LD_LIBRARY_PATH = /u01/app/oracle/product/10.2.0.4/db/lib


oracle@[/u01/app/oracle] ps -ef |grep pmon
oracle 7690 1 0 Jun07 ? 00:00:10 ora_pmon_inftst1
oracle 17588 1 0 May05 ? 00:02:15 ora_pmon_tstrsi1
oracle 26806 1 0 Aug02 ? 00:00:01 ora_pmon_tstrst1
oracle 31816 30627 0 16:44 pts/0 00:00:00 grep pmon
oracle@[/u01/app/oracle]
oracle@[/u01/app/oracle] setsid tstrsi1


ORACLE_SID = tstrsi1
ORACLE_HOME = /u01/app/oracle/product/10.2.0.4/db
PATH = /usr/kerberos/bin:.:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.:/usr/sbin:/u01/app/oracle/product/10.2.0.4/db/bin:/usr/openwin/bin:/u01/app/oracle/utils:/u01/app/oracle/product/10.2.0.4/db/rdbms/admin:/u01/app/oracle/product/10.2.0.4/db/network/admin:/u01/app/oracle/product/10.2.0.4/db/OPatch
ORACLE_BASE = /u01/app/oracle
ORA_NLS33 = /u01/app/oracle/product/10.2.0.4/db/ocommon/nls/admin/data
LD_LIBRARY_PATH = /u01/app/oracle/product/10.2.0.4/db/lib


oracle@[/u01/app/oracle] showsid


ORACLE_SID = tstrsi1
ORACLE_HOME = /u01/app/oracle/product/10.2.0.4/db
PATH = /usr/kerberos/bin:.:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:.:/usr/sbin:/u01/app/oracle/product/10.2.0.4/db/bin:/usr/openwin/bin:/u01/app/oracle/utils:/u01/app/oracle/product/10.2.0.4/db/rdbms/admin:/u01/app/oracle/product/10.2.0.4/db/network/admin:/u01/app/oracle/product/10.2.0.4/db/OPatch
ORACLE_BASE = /u01/app/oracle
ORA_NLS33 = /u01/app/oracle/product/10.2.0.4/db/ocommon/nls/admin/data
LD_LIBRARY_PATH = /u01/app/oracle/product/10.2.0.4/db/lib



Nirmal
 
Old 08-04-2010, 09:10 AM   #6
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Thanks Nirmal, that's helpful (code tags would have been nice).

It shows that running setsid results in a change of showsid output. It does not show the effect of running setsid.sh.

What is setsid? The command type setsid should show. What happens if you repeat as above, using setsid.sh instead of setsid?
 
Old 08-04-2010, 11:21 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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.
 
Old 08-04-2010, 12:26 PM   #8
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
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.


Nirmal
 
Old 08-05-2010, 06:38 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What is setsid? The command type setsid should show. What happens if you repeat as above, using setsid.sh instead of setsid?
 
Old 08-05-2010, 12:23 PM   #10
nsingh49
LQ Newbie
 
Registered: Aug 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Catkin,
It will show the same result..

Nirmal
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't set root env variables in script? voyciz Slackware 3 09-23-2009 07:44 PM
how to set global environment variables? demia Linux - Server 6 04-29-2009 11:29 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
global environment variables in shell script a.toraby Programming 2 11-11-2007 03:25 AM
How make a script in order to set global alias? RickHunter_Ve Linux - Newbie 7 06-08-2004 02:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Red Hat

All times are GMT -5. The time now is 06:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration