Script export problem in FC3

Nifty Hat Mitch mitch48 at sbcglobal.net
Sun Jan 2 06:06:39 UTC 2005


On Tue, Dec 21, 2004 at 11:43:27PM +0100, Samuel Díaz García wrote:
> 
> I need to modify the PATH environment using a script. My script is:
> 
> --begin--
> #!bin/sh
> export PATH=$PATH:/mydir
> --end--
> 
> If I run the export command in command line I have no problem, but when 
> I run my script, and I run
> 
> echo $PATH
> 
> I can see that the path environtment variable isn't changed.
> 
> What can be the problem?
> I'm root user when I run the script.
You should not need to be root.

The problem is that variables in the environment 
are passed from parent to child.  Not to the system
or from child to parent.

The pseudo comment
   > #!.bin/sh
instructs exec to fork a child process bin/sh
with the input that follows.
   > export PATH=$PATH:/mydir

The child process has the new environment, not the 
parent of the process. ps lets you see the partent
child relationship.  Note how 646 is in different collums
below.
   $ ps -l
   F S   UID   PID  PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
   0 S   500   646 11293  0  75   0 -  1323 wait   pts/2    00:00:00 bash
   0 R   500   669   646  0  77   0 -   971 -      pts/2    00:00:00 ps

The environment of a process you own/conftol can be seen in the /proc
filesystem.

    $ ls /proc/646
    attr  cmdline  environ  fd    mem     root  statm   task
    auxv  cwd      exe      maps  mounts  stat  status  wchan

You can have your child process sleep and then go have a 
look in the /proc filesystem.
   #!/bin/sh
   export PATH=$PATH:/mydir
   sleep 3600 # about an hour


What you are attempting to do is commonly done in a dot file
in the users home directory.
   $ grep PATH .[a-z]*
   .bash_profile:PATH=:$HOME/bin:$PATH:.:

Next time you login this will be included as part of the 
normal start up for a user.

The shell "csh" has a command "source" and bash and sh use
the notation . to include shell command in a script.
You will see this in /etc/init.d scripts by way of example.

     # Source function library
     if [ -f /etc/init.d/functions ]; then
       . /etc/init.d/functions
     elif [ -f /etc/rc.d/init.d/functions ] ; then
       . /etc/rc.d/init.d/functions
     else
       exit 0
     fi

You will also see in /etc/bashrc a snip of shell code that
picks up commonly used stuff from /etc/profile.d/
     for i in /etc/profile.d/*.sh; do
		if [ -r "$i" ]; then
		    . $i
		fi
	    done
     unset i

If you follow this strategy you will find a template for adding
stuff to PATH in this /etc/profile.d/krb5.sh that does not
keep adding duplicate stuff to $PATH.

Look also at /etc/bashrc where a test for the user id is
made.  In general you do not want to modify PATH for the root
and other system accounts as it opens up a door for trojan horse
programs.



-- 
	T o m  M i t c h e l l 
	spam unwanted email.
	SPAM, good eats, and a trademark of  Hormel Foods.




More information about the fedora-list mailing list