Re: Script export problem in FC3

From: Nifty Hat Mitch (mitch48_at_sbcglobal.net)
Date: 01/02/05

  • Next message: Gene Heskett: "Re: firefox question re mpg download disposal"
    Date: Sat, 1 Jan 2005 22:06:39 -0800
    To: For users of Fedora Core releases <fedora-list@redhat.com>
    
    

    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.
    -- 
    fedora-list mailing list
    fedora-list@redhat.com
    To unsubscribe: http://www.redhat.com/mailman/listinfo/fedora-list
    

  • Next message: Gene Heskett: "Re: firefox question re mpg download disposal"

    Relevant Pages

    • Re: user id validation
      ... I have not tried like that but I feel it can be done because the environment ... Rajnikant Jachak | Software Engg | Persistent Systems Limited ... I am just wondering how to validate a user who is using the script?? ... there specific perl command for that? ...
      (perl.beginners)
    • Re: rm -r /usr/local by mistake
      ... (i.e. act the _same_ way, for the same invocation, in any environment). ... In the non-interactive language there is *no* "rm' command, ... So in a noninteractive script, ... Do you understand that "would have to specify the 'non-interactive' flag" ...
      (comp.unix.bsd.freebsd.misc)
    • Re: Help with system seeming to return too soon...
      ... >>install script. ... > a new process, sets the environment variables in that process, then ... I can on the command line) that I could get around this problem. ...
      (comp.lang.perl.misc)
    • Re: using ` and the bash source command
      ... As part of a script. ... This doesn't fail with the command not found, ... Does anyone have a workaround for getting ruby to exec the bash source ... So, it is running bash in a child process, ...
      (comp.lang.ruby)
    • RE: DBI->Connect returning UNDEF in cgi but not in standalone pro g
      ... script where it worked to another script - and screwed up by just typing ... Thats why standalone perl program worked from ... So as per Ron and Bill, I also agree that environment variables might be ... >> The fact your code executes from the command line and not the web ...
      (perl.dbi.users)