LinuxQuestions.org
Visit Jeremy's Blog.
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 06-12-2008, 12:05 AM   #1
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Rep: Reputation: 0
can't run source /etc/profile from script file? any help would be great!


I am writing this script to change you current path, but it will not change the $PATH unless I type source /etc/profile in the terminal or if I exit the terminal and log back in. any more sujestion for doing this from a script file? Thanks.
 
Old 06-12-2008, 12:16 AM   #2
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
Tree of processes.

Quote:
Originally Posted by icecoolcorey View Post
I am writing this script to change you current path, but it will not change the $PATH unless I type source /etc/profile in the terminal or if I exit the terminal and log back in. any more sujestion for doing this from a script file? Thanks.
I'm not sure what your exact problem is.

Think to check number 1. What shell are you running? /bin/sh? /bin/bash?

Try
echo $SHELL

and

ls -l `echo $SHELL`

Should be informative.

One common gotcha with scripting is to expect the variables to persist if you invoke a sub script.

Nope, what happens when you run a script (effectively, there is some very fine fineprint on this) is a new process is created, your current environment variables that are flagged as "export" are copied to the memory of the new process.

That new process may create new environment variables, it make invoke and hence export those variables to it's children, but when it exits.

Bang! It's memory is recovered and any environment variables are lost and not taken back to the parent.

The only way for a script to set environment variables in the current process, is to source it, to run and interpret the script in the current process.

Thus doing this....
./myScript
...will create a new process, run ./myScript exit the process and lose the envinoment variables.

Doing this...
source ./myScript
or equivalently this...
. ./myScript
...will redirect the stdin of your current process to that file, until all the commands have been read and executed, and then point it back at your current terminal, leaving all environment variables changed by ./myScript visible to you.

I hope that helps.
 
Old 06-12-2008, 12:29 AM   #3
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
I am using the /bin/bash shell.
so I would put source /etc/profile in Myscript
then in my current script file I should end it with..

./Myscript

Will this run the command (source /etc/profile)like you would put in a the terminal?
 
Old 06-12-2008, 01:39 AM   #4
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
Quote:
Originally Posted by icecoolcorey View Post
I am using the /bin/bash shell.
so I would put source /etc/profile in Myscript
then in my current script file I should end it with..

./Myscript

Will this run the command (source /etc/profile)like you would put in a the terminal?
Wha? I'm a little confused about what you are trying to do there... You seem to be doing something strange, can you be more explicit. eg. Copy and paste lines of code and terminal sessions.

Having...
source /etc/profile
in Myscript will run the commands that are contained in /etc/profile in the whichever process Myscript is running in.

So running Myscript like so...
./Myscript

Will create a new process, run the commands in /etc/profile creating and change the variables that /etc/profile affects in _that_ process. When the script ends, the process ends, the memory is recovered and the variables are lost. And you find the variables you hope should be set in your terminal session aren't.

If you run Myscript like so from your terminal session....
source ./Myscript
or equivalently...
. ./Myscript
(note the dot and the space there) the commands in the file Myscript are interpreted by the process controlling your terminal session, including the command "source /etc/profile" hence the commands in "/etc/profile" are interpreted by the process running your terminal session.

Once it is finished eating commands from the file, the same process goes back to eating commands from your keyboard and the variables created and set by those commands you just ran are still there and will affect everything else you run.

(Well... their is fine print on that too, only those variables flagged to be "export"ed will be passed on to future subprocesses.)
 
Old 06-12-2008, 02:51 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.10, Centos 7.5
Posts: 17,565

Rep: Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425Reputation: 2425
In addition to cyent's comments;

/etc/profile is run every time you login, before it gets to your personal (hidden) files eg .bashrc, .bash_profile (or .profile) .
To change the final PATH var, (which is usually mentioned in one of those 2), just use

PATH=$PATH:/your/new/path
export PATH

in the appropriate profile file.
 
Old 06-12-2008, 03:18 AM   #6
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
thanks for you advise!
I am trying to make the PATH update though the /etc/profile. I append
PATH=$PATH:$ADDPATH like so
echo "PATH=$PATH:$ADDPATH" >> /etc/profile

after this I would like the script file to update the PATH (from the script file) so it can be used again in the script file to add or remove file PATHs.

I have tryed the export before. it did not work right(or at all)so I am trying it a different way.
dose this make any sense?

Last edited by icecoolcorey; 06-13-2008 at 02:29 AM. Reason: didn't have >> to append text to /etc/profile
 
Old 06-12-2008, 05:06 PM   #7
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
Wow, that's harsh...

Quote:
Originally Posted by icecoolcorey View Post
thanks for you advise!
I am trying to make the PATH update though the /etc/profile. I append
PATH=$PATH:$ADDPATH like so
echo "PATH=$PATH:$ADDPATH" > /etc/profile
Usually distributions put useful stuff into your /etc/profile.

echo "PATH=$PATH:$ADDPATH" > /etc/profile

Is going to just clobber /etc/profile leaving nothing but that path assignment. That's a bit harsh and user unfriendly...

You may mean ....
echo "PATH=\$PATH:$ADDPATH" >> /etc/profile
Which would just append the new path to the end of the /etc/profile

Also you clearly are not a very experienced scripter... yet you are doing things that require working with root priviledge. This is a really good way of doing amazing amount of damage to your system. Sort of like skating with a chainsaw.. (I saw somebody doing that the other day.... inline skating down a busy sidewalk with a chainsaw in his hand. Fortunately it wasn't running, but still... makes running with scissors seem tame.) (I am an experience scripter and I _never_ debug a script whilst running as root. The main thing experience teaches you is when to cringe...)

I would recommend not using root, get the script sorted targeting ~/.profile instead. Once you have the script debugged and working you may wish to revisit the issue using root and /etc/profile.

It could be that having clobbered /etc/profile as I mentioned above you're ending up with PATH not being exported so either the append trick or adding export may help.
echo "export PATH=\$PATH:$ADDPATH" >> /etc/profile

Quote:
after this I would like the script file to update the PATH (from the script file) so it can be used again in the script file to add or remove file PATHs.
Maybe regard it as too separate things and two separate actions. Updating /etc/profile and changing the current path.

And now I'll give you the best scripting advice you'll ever receive...

Stop doing bash, bash is a 20 year old lowest common denominator solution to a different problem. As a programming language it truly stinks.

Move on to one of the modern generation perl, python, ruby, scheme,...

My personal preference is Ruby, perhaps followed by python.

You'll save yourself _so_ much sorrow, and be way more productive.
 
Old 06-13-2008, 12:11 AM   #8
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
pathchanger

I finished the script file it is simple, but I only have 3 mouth working with linux. I made it on fordora 8. So it can be used with any /bin/bash or bash shell to change and remove your current path. here is the link if you would like to see what it does. URL="http://www.icecoolcorey.net/linux/chpath.tar.gz"]
http://www.icecoolcorey.net/linux/chpath.tar.gz[/URL]

Last edited by icecoolcorey; 06-13-2008 at 12:14 AM.
 
Old 06-13-2008, 12:15 AM   #9
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
http://icecoolcorey.net/linux/chpath.tar.gz
 
Old 06-13-2008, 10:00 PM   #10
sam_o_rogers
Member
 
Registered: Apr 2006
Posts: 47
Blog Entries: 1

Rep: Reputation: 15
Old Stuff

[QUOTE=cyent;3182959]Usually distributions put useful stuff into your /etc/profile.


Stop doing bash, bash is a 20 year old lowest common denominator solution to a different problem. As a programming language it truly stinks.

OK,

I guess you would call me a hacker, ie, I pick up stuff that works, as opposed to going about a structured way learning all about something. On occasion, I will sometimes read a book, like a system 5 reference manual, which is a good way to fall asleep, but if you don't use it all the time, it drops down the bit bucket.

That being said, I come from a situation where environment variables are important. In the old days, you could assign an environment variable and export it in your profile, and when you logged in, it was set automatically. Saves a lot of typing from the command line. With Linux, I discovered it wasn't .profile, it was .bash_profile. Very well, I can do that.

Now however, it appears that .bash_profile is not getting executed when I log in with a user ID. I suppose I could telnet to localhost and make sure the script gets executed that way, except with this current Ubuntu install, remote logins are disabled, which appears to extend to telneting to local host.

So, if I do a source ./.bash_profile, I do get the environment variables set for the running terminal session. But that still doesn't set the environment varialble for everything running when I logged into the Gnome session to begin with.

What am I missing? Where does one set environment variables to be applied for everything that is running when you log in as a user?
 
Old 06-13-2008, 11:06 PM   #11
icecoolcorey
LQ Newbie
 
Registered: Jun 2008
Posts: 19

Original Poster
Rep: Reputation: 0
To ensure that the variables are placed into memory each time the user logs in, you must put them in a enviromnment file. The order that these file are executed are:
/etc/profile
~/.bash_profile
~/.bash_login
~/.profile
if you put the varable that you want in one of these files then do an export like you said it should work.

Last edited by icecoolcorey; 06-14-2008 at 12:40 AM.
 
Old 06-15-2008, 05:00 PM   #12
cyent
Member
 
Registered: Aug 2001
Location: ChristChurch New Zealand
Distribution: Ubuntu
Posts: 346

Rep: Reputation: 76
Quote:
Originally Posted by sam_o_rogers View Post
Now however, it appears that .bash_profile is not getting executed when I log in with a user ID. I suppose I could telnet to localhost and make sure the script gets executed that way, except with this current Ubuntu install, remote logins are disabled, which appears to extend to telneting to local host.
All xterm variants I have seen so far have something like this...

On gnome-terminal it's Edit Menu->Current Profile->Title and Command Tab->Command Section->Run command as loginshell tick box.

Set that on.

On xterm variants closer to the original it is the "-ls" command line option.
 
Old 06-17-2008, 08:20 PM   #13
sam_o_rogers
Member
 
Registered: Apr 2006
Posts: 47
Blog Entries: 1

Rep: Reputation: 15
Dear Cyent,

Thank you for the response. See, you can too teach a very old dog new tricks.

When you stop learning something new, it's time to reach room temperature.
 
  


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
How to run bat file from Unix script??? gopi_20us Programming 10 09-14-2010 10:15 AM
How do I get a script file to run automatically when linux boots? kccrusher Linux - Software 4 11-13-2006 10:55 PM
Automatically run script on file? paddyjoy Linux - Software 3 10-03-2006 05:29 PM
how to run a script on subfolder file in a folder saggi_sam Linux - Newbie 5 07-13-2005 06:04 AM
Autorun.sh script to mount CD and run .bin file kulkarni08 Linux - General 2 06-24-2003 02:01 PM

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

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