LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-14-2011, 05:11 PM   #1
PoleStar
Member
 
Registered: Jul 2010
Posts: 170

Rep: Reputation: 2
shell script "export" for other users


Hi,
Code:
#!/bin/bash


export CLASS=shoultNotBethere

su -m testuser  -c "export CLASS=TherightOne"

su -m testuser -c echo $CLASS

echo $CLASS

exit
It does not bring back any thing.

So basically I need to set environment variable for test user.
But I have to run the command as root.

Any Ideas ?
 
Old 12-14-2011, 05:48 PM   #2
eSelix
Senior Member
 
Registered: Oct 2009
Location: Wroclaw, Poland
Distribution: Arch, Kubuntu
Posts: 1,254

Rep: Reputation: 314Reputation: 314Reputation: 314Reputation: 314
Do it in his starting script for example ~/.bashrc or globally in /etc/environment or other places, depending on your system. If you want to set environment variable for only one command, do it just before executing it, like
Code:
VAR=value ./program
You can't change your initial environment from some script runned later. The calling program set environment for his childs, but called program can't do that for his parent.

Last edited by eSelix; 12-14-2011 at 05:53 PM.
 
Old 12-14-2011, 09:03 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 373Reputation: 373Reputation: 373Reputation: 373
I'll try to elaborate on what eSelix said.

When you run your script, it creates a separate, distinct subshell:
Original CLI shell -> Script subshell

When your script executes
Code:
su -m testuser  -c "export CLASS=TherightOne"
you are telling the computer that you want to spawn another shell and execute the command(s) following the -c
Original CLI shell -> Script subshell -> "dash c" sub-subshell

The export command takes place, and the environment in the "dash c" sub-subshell is modified. The "dash c" sub-subshell closes immediately after the export completes.

As eSelix said, but phrased a little differently, a child/subshell inherits the environment from its parent shell. But a child/subshell cannot push back environment changes to a parent.

For that reason, the export command in the "dash c" sub-subshell lives and dies in that shell alone.

Last edited by Dark_Helmet; 12-14-2011 at 09:05 PM.
 
Old 12-15-2011, 01:47 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
It can be done if the command run by su writes the desired value to stdout:
Code:
export CLASS=$( su testuser -c "echo TheRightOne" )
 
Old 12-15-2011, 03:13 AM   #5
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
alternatively :

skip following line and it will work

Code:
#!/bin/bash
 
Old 12-15-2011, 04:15 AM   #6
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
@asimba are you sure that will work.....
 
Old 12-15-2011, 05:11 AM   #7
asimba
Member
 
Registered: Mar 2005
Location: 127.0.0.0
Distribution: Red Hat / Fedora
Posts: 355

Rep: Reputation: 42
Quote:
Originally Posted by bsat View Post
@asimba are you sure that will work.....
Try it
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] Errors executing shell script: "command not found" and "no such file or directory" eko000 Linux - Newbie 1 01-14-2011 08:54 AM
[SOLVED] Shell scripting, difference of "source script.sh" and "./script.sh" Squall90 Programming 4 07-31-2010 05:40 PM
Shell script: I have string "abc____def____ghi", how to make "abc def ghi" vouser Programming 8 03-09-2010 11:01 PM
making shell "export" variable permanent forever: user/system lambchops468 Linux - Software 4 10-13-2009 06:37 AM
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration