LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-11-2008, 06:49 PM   #1
spork
LQ Newbie
 
Registered: Feb 2003
Location: MA
Distribution: Fedora 8; Ubuntu Jaunty
Posts: 7

Rep: Reputation: 0
bash script and exporting variables into a subshell


Hi there,
I have a bash script that I grabbed from work which checks that the user running the script is a specific user (we'll call the user foo). If the user is not foo then the current user name is exported to an environment variable and the script re-executes through sudo as foo (sudo -u foo $0 $*).

So far so good, however when the script gets re-run the environment variable no longer exists. It runs fine at work (REL, but I don't know which version), but not locally (Fedora 8).

My suspicion is that sudo as a different user is causing me to lose the environment variable, but that doesn't sound right either. Any hints?

Code:
#!/bin/bash

echo "0--owner=$OWNER"
echo "0--user=$USER"
if [ "$1" = "" ]; then
    echo "Usage: foo <bar>"
    exit;
fi

base=/foo/bar
if [ ! -d "$base" ]; then
    echo "Base directory '$base' does not exist"
    exit 1
fi

if [ "$USER" != "foo" ]; then
    export OWNER=$USER
    echo "1--owner=$OWNER"
    sudo -u foo $0 $*
    echo "DONE"
    exit;
fi

echo "2--owner = $OWNER"
if [ "$OWNER" = "" ]; then
    OWNER=$USER
fi

echo "3--owner = $OWNER"

...
<meat of the script>
Results of execution:
0--owner=
0--user=john
1--owner=john
0--owner=
0--user=foo
2--owner =
3--owner = foo

Last edited by spork; 10-11-2008 at 08:16 PM.
 
Old 10-11-2008, 07:42 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I think the env at work isn't what you think.
Its a fundamental of shells that you can only create an env var into the current env, and optionally export it to all child shells created from the current shell.
However, as soon as the 'current'/parent shell exits(!), all new env vars created are destroyed.
IOW, you can't export env vars 'upwards'.
Every time you run a shell prog it crates a new (sub) shell to run in. It doesn't run in the env its called from.
You can get env vars to remain if you source the code instead of running it normally ie

source some_shell_prog

OR

. some_shell_prog

http://www.tldp.org/LDP/abs/html/subshells.html

HTH
 
Old 10-11-2008, 08:15 PM   #3
spork
LQ Newbie
 
Registered: Feb 2003
Location: MA
Distribution: Fedora 8; Ubuntu Jaunty
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks Chris. That confirms what I was thinking.

What is odd is that it works at work. OWNER is used later on in the script and is available with the original caller's name.

I also realize I made a typo in the code section where the username in the sudo line is wrong. I will correct that now.

Thank you for the link.
 
Old 10-14-2008, 11:38 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
Hi Spork,

The sudo had recently improved its security rules. One of the more devastating things to me was the clear of the environment variables as part of this policy. You can do "man sudo" and look for "preserve environment" and "setenv".

RHEL 4 and/or 4 is perhaps a relatively old distribution and uses the old sudo behavior. Fedora 8 is the distribution I use and it's where the things are broken.

Osvaldo.
 
  


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
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
bash script, understanding variables antis Programming 5 11-15-2007 04:27 AM
Using Bash Script for Exporting and Returning Environmental Variables Jicksta Programming 3 12-04-2004 04:14 PM
bash script variables twantrd Programming 7 11-17-2004 02:38 AM
exporting variables under bash isn't working! dtheorem Programming 6 03-11-2004 08:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:33 PM.

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