Extracting a users environment variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting a users environment variables
# 1  
Old 12-04-2006
Extracting a users environment variables

Hi Guys,

I want to extract users environment variables via a sh script, and for some reason it is not working.

According to the su man page:
Code:
     Example 3: Executing command with user bin's Environment and
     Permissions

     To execute command with the temporary environment  and  per-
     missions of user bin, type:

     example% su - bin -c "command args"

But this doesn't work for me. What I expect to see is /opt/bb/bbhome , But I get nothing
Code:
# su - bb -c "echo $BBHOME"
Sun Microsystems Inc.   SunOS 5.9       Generic May 2002

Operating System Version 5.9.01



#

I can see the settings but I can't extract just one.
Code:
# su - bb -c env
Sun Microsystems Inc.   SunOS 5.9       Generic May 2002

Operating System Version 5.9.01


PWD=/opt/bb
TZ=Australia/Victoria
HZ=
HOSTNAME=s00071
MACHTYPE=sparc-sun-solaris2.9
MAIL=/var/mail/bb
BBHOST=s00071
LARRDHOME=/opt/bb/larrd
LOGNAME=bb
SHLVL=1
SHELL=/bin/bash
HOSTTYPE=sparc
BBHOME=/opt/bb/bbhome
OSTYPE=solaris2.9
HOME=/opt/bb
TERM=xterm
PATH=/opt/bb/bbhome/bin:/usr/local/bin:/usr/bin:/usr/ucb:/etc:.:/usr/ccs/bin
_=/usr/bin/env

#

I want bbhome to be /opt/bb/bbhome and then use bbhome in a script.
Code:
# bbhome=`su - bb -c "env | grep BBHOME"`
# echo $bbhome
Sun Microsystems Inc. SunOS 5.9 Generic May 2002 Operating System Version 5.9.01 BBHOME=/opt/bb/bbhome
#

So does anyone know why the -c option in su is not working ?
I don't want to write a function just to set a variable, since the -c option should do this.
Tornado
# 2  
Old 12-05-2006
When you use double quotes, your interactive shell will replace the BBHOME variable before the su command starts to run.
# 3  
Old 12-05-2006
It makes no difference
Code:
# su - bb -c echo $BBHOME
Sun Microsystems Inc.   SunOS 5.9       Generic May 2002

Operating System Version 5.9.01



#

If I use single quotes I have the same problem as when I used grep above
Code:
# bbhome=`su - bb -c 'echo $BBHOME'`
# echo $bbhome
Sun Microsystems Inc. SunOS 5.9 Generic May 2002 Operating System Version 5.9.01 /opt/bb/bbhome
#

Tornado
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Environment Variables

1. The problem statement: What is the mesg value set for your environment? If it is on, how would you turn off your current session? How would you set it permanently? 3. The attempts at a solution : Read Unix The textbook. 3rd chapter has many things like environment variables and... (5 Replies)
Discussion started by: mahinkhan22
5 Replies

2. Shell Programming and Scripting

Links, using environment variables, with the same name, for all users at the same time

Hi all! I have the following problem: I want to create a symbolic link at the root: /a which points to the folder: /b/MyFiles It's simple, but here comes the troubling part. If I log in with an other user at the same time, I would like to see the following under a: /b/HisFiles so... (1 Reply)
Discussion started by: Emri Balázs
1 Replies

3. Shell Programming and Scripting

using environment variables

say i define an environment variable in a particular script (upgrade.sh). my script is upgarde.sh and it calls another script try.sh. will this environment variable be accessible to try.sh also. if not how to I make environment variables global so that they can be used by any script. (2 Replies)
Discussion started by: lassimanji
2 Replies

4. UNIX for Dummies Questions & Answers

Can I export the users environment using sudo?

I want the user to be able to commands as another user.. but when they do that.. I need them to have the environment variables of the other user. is this possible with sudo? sudo -H -u user env 'env' is giving me the environment of the current user, not the user I want to run commands as. ... (1 Reply)
Discussion started by: julesdiane
1 Replies

5. Solaris

Add environment variables for all users

hi I need to add environment variables for all users and make them available when they login. I don't know which file to edit. Also these variables should be displayed when "env" command is executed. Please help....which file is common for all users, without editing their specific .profile or... (2 Replies)
Discussion started by: ashish_uiit
2 Replies

6. UNIX for Advanced & Expert Users

Managing Users in a Global Environment

Hello, I am interested in your strategy for handling engineers Unix accounts when the engineers must log in to resources in a variety of locals in a global environment. The engineers home directory and normal environment is local to where the engineer is sitting. When they log in to a remote... (0 Replies)
Discussion started by: Randal
0 Replies

7. Shell Programming and Scripting

environment variables

Hi, If i have a variable set and exported in my pofile file will that variable be available in all shell scripts created. Thanks, Radhika. (3 Replies)
Discussion started by: radhika03
3 Replies

8. Programming

environment variables

hi, I want to create a new EV(Environment Variable) through a c program and I done this thing through setenv() method. But the newly created EV is not permanent, i.e. when I exit from the program the EV also no longer lives. But I want to make it a permanent EV for the current user. Actually I... (6 Replies)
Discussion started by: sumsin
6 Replies

9. UNIX for Dummies Questions & Answers

help..Environment variables...

hi, 1). i would like to know what is meant by environment variables? 2). is the number of envi variables is a constant number for unix systems? 3). how to see the list of envi variables (and the values of the envi variables)in a single command? 4). if this questions were already asked... (3 Replies)
Discussion started by: sekar sundaram
3 Replies

10. Programming

environment variables

Hi! How-to get the environment variables in GNU. getenv() only fetches the ones that you can find under export (not the ones under declare)... best regars .David (2 Replies)
Discussion started by: Esaia
2 Replies
Login or Register to Ask a Question