LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-13-2009, 03:01 PM   #1
Moonman
Member
 
Registered: May 2004
Posts: 37

Rep: Reputation: 15
How to get environment variable from other user in bash script?


I am trying to run a bash script as root to test/obtain the $ORACLE_HOME environment variable under the oracle user's profile.

Anyone know of a good way to script that?

I tried using sudo su - oracle "echo $ORACLE_HOME" but that doesn't work.
 
Old 01-13-2009, 05:20 PM   #2
FragInHell
Member
 
Registered: Sep 2003
Location: Sydney Australia
Distribution: Redhat, Centos, Solaris, Ubuntu, SUSE
Posts: 282

Rep: Reputation: 45
Try Grep'ing the variable from there .bashrc script instead.
cat /home/oracle/.bashrc | grep ORACLE_HOME | cut -d"=" -f2

I've not tested this but what I'm trying to do would be
cat the Oracle users .bashrc file
Look for a string with ORACLE_HOME then strip of the fist part of the script so only get the path
so in the .bashrc file it might have some thing like export ORACLE_HOME=/some/path
to Put this all into a variable to use in your script try this:

MyOracleHome=`cat /home/oracle/.bashrc | grep ORACLE_HOME | cut -d"=" -f2`

As I say not tested, but hopefully might point you in the right direction, let me know how you go...
 
Old 01-13-2009, 05:21 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Something like 'sudo su -l oracle -c env|grep HOME'?
 
Old 01-13-2009, 06:24 PM   #4
Moonman
Member
 
Registered: May 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks unSpawn, that did the trick. Anyone know why echo doesn't work? Just curious...

Last edited by Moonman; 01-13-2009 at 06:27 PM.
 
Old 01-13-2009, 08:09 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,369

Rep: Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753Reputation: 2753
If you are root, you don't need sudo, just su -. Also, re echo, you need the -c flag to tell it its a cmd...

Quote:
-c, --command=COMMAND
pass a single COMMAND to the shell with -c
 
Old 01-13-2009, 11:22 PM   #6
Moonman
Member
 
Registered: May 2004
Posts: 37

Original Poster
Rep: Reputation: 15
su in a script stops the script. sudo su - allows a script to execute a command and continue running. Even with the -c command, echo doesn't work.

Last edited by Moonman; 01-13-2009 at 11:29 PM.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to export environment variable from a bash script dimaash Linux - Newbie 20 08-09-2011 07:12 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
how to unset environment variable in bash suneel Linux - Newbie 7 09-14-2009 10:17 AM
Bash Script Help - Trying to create a variable inside script when run. webaccounts Linux - Newbie 1 06-09-2008 02:40 PM
1:root vncserver: The USER environment variable is not set. Niceman2005 Linux - Software 1 05-24-2006 10:13 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 05:46 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