LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-16-2011, 09:57 AM   #1
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Rep: Reputation: 1
/etc/profile file


Hi

I found the following function in /etc/profile file.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code:
pathmunge () {
    if ! echo $PATH | /bin/grep -qE "(^|:)$1($|:)" ; then
       if [ "$2" = "after" ] ; then
          PATH=$PATH:$1
       else
          PATH=$1:$PATH
       fi
    fi

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

1. I dont undestand what "if ! echo $PATH | /bin/grep -qE "(^|:)$1($|:)"" this if statement actually comapres??

2. Also what is the difference between PATH=$PATH:$1 & PATH=$1:$PATH

Please guide me.Thanks in advance Soji.

Last edited by Soji Antony; 01-17-2011 at 06:31 AM.
 
Old 01-16-2011, 10:14 AM   #2
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 672

Rep: Reputation: 94
Quote:
Originally Posted by Soji Antony View Post
Hi

2. Also what is the difference between PATH=$PATH:$1 & PATH=$1:$PATH

Please guide me.Thanks in advance Soji.
Difference is in the look up turn. A simple example;
$PATH=/usr/bin:/bin
shell will look first in /usr/bin, if It can't find it in /usr/bin then It will look in /bin
This is important if there is a command override some where.
 
1 members found this post helpful.
Old 01-16-2011, 10:16 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Try editing your post and making use of the CODE tag feature. That way your code will be more readable. For example some it won't be translated in to smiley faces.
 
1 members found this post helpful.
Old 01-16-2011, 10:39 AM   #4
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
ok ... thanks for your quick reply ... But why is this path difference assigned for root and a normal user ..

root : PATH=$1:$PATH
normal-user : PATH=$PATH:$1

???

Last edited by Soji Antony; 01-16-2011 at 10:41 AM.
 
Old 01-16-2011, 11:41 AM   #5
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 672

Rep: Reputation: 94
That depends on what $1 is.
 
1 members found this post helpful.
Old 01-16-2011, 12:50 PM   #6
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by Soji Antony
1. I dont undestand what "if ! echo $PATH | /bin/grep -qE "(^|:)$1($|:)"" this if statement actually comapres??
If you don't understand a command, then start by breaking it into smaller pieces, figure out the pieces, and then see how all the pieces fit back together.

Here, you have three blocks: (1) an if statement; (2) an echo; and (3) a grep.


I'll start with the grep, because that's probably what's confusing. So, check the options given to grep:
From man grep:
Code:
       -E, --extended-regexp
              Interpret PATTERN as an extended regular expression (ERE, see below).  (-E is specified by POSIX.)

       -q, --quiet, --silent
              Quiet;  do  not  write anything to standard output.  Exit immediately with zero status if any match is found, even if an error was detected.  Also see the -s or --no-messages option.  (-q is
              specified by POSIX.)
So that leaves the regular expression: "(^|:)$1($|:)". Again, break it into pieces:

(^|:) - the parentheses do not match a character. They only isolate what's inside from the rest of the regular expression. The bar '|' means OR. As in, look for a match of either '^' or ':'. The '^' represents the beginning of a line, and the ':' represents itself--an actual colon. So this piece means to match EITHER the beginning of a line or a colon.

$1 - the $1 variable refers to the first argument given to the function.

($|:) - Again, the parentheses do not match a character, the bar '|' means OR, and the ':' matches an actual colon. The '$' represents the end of a line. So this piece means to match EITHER the end of a line or a colon.

Put all the pieces together, and grep is looking to match (1) the beginning of a line or a colon; followed by (2) the text of the first argument passed to the function; followed by (3) the end of a line or another colon.

Because of the '-q' option, grep will not print its results, but exit with a value of 0 if a match is found.



The echo command is simple. It simply sends the text of the PATH environment variable to grep. Keeping in mind that the PATH environment variable generally looks like: "/bin:/usr/bin:/usr/local/bin", you should start to see why the grep is looking for the pattern that it is.



The if statement is also straightforward, but has one tiny wrinkle. The if statement uses the exclamation '!' to invert the results of the echo-grep. Remember that if grep finds a match, it exits with a 0. The exclamation inverts the 0, turning it into a non-zero value. Keep in mind that, in the shell, a 0 is considered success/true and non-zero is considered false. So, if a match is found (grep returns 0--success), the if statement inverts it (to non-zero--false), and the interior of the if statement is not executed.

In other words, the interior of the if statement is run only when the grep does not find a match.



Quote:
Originally Posted by Soji Antony
2. Also what is the difference between PATH=$PATH:$1 & PATH=$1:$PATH
ozanbaba is right. If $PATH:$1 is used, then $1 (again, $1 is the first argument given to the function) is placed at the end of PATH meaning it will be the last directory searched when looking for a command. Conversely, if $1:$PATH is used, then $1 is placed at the beginning of PATH and will be the first directory searched.


If you look at the function as a whole, it's taking these steps:
1. Check if the directory specified in $1 already exists in the PATH
2. If the directory already exists: do nothing and exit
3. If the directory is missing, add it to PATH
3a. Put the new directory at the end of PATH if $2 contains the text "after"
3b. Put the new directory at the beginning of PATH if $2 contains any other text



Quote:
Originally Posted by Soji Antony
ok ... thanks for your quick reply ... But why is this path difference assigned for root and a normal user ..

root : PATH=$1:$PATH
normal-user : PATH=$PATH:$1

???
None of the code you posted has anything to do with PATH being modified by root or a normal user. This function will work the same way for either root or a normal user.

Last edited by Dark_Helmet; 01-16-2011 at 12:56 PM.
 
2 members found this post helpful.
Old 01-17-2011, 04:08 AM   #7
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Hi

Many thanks for your elaborate answer. Actually , this is the full code

Code:
pathmunge () {
    if ! echo $PATH | /bin/grep -qE "(^|:)$1($|:)" ; then
       if [ "$2" = "after" ] ; then
          PATH=$PATH:$1
       else
          PATH=$1:$PATH
       fi
    fi
}

# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi
If I am not wrong , root user [ $EUID=`id -u`] having user id 0 , gets this path PATH=$1:$PATH and all other users get PATH=$PATH:$1. So this is my question , why there is a difference in path for a normal user and a root user ???

Thanks in advance ...

Last edited by Soji Antony; 01-17-2011 at 11:15 AM.
 
Old 01-17-2011, 04:30 AM   #8
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Did you read my previous post about using CODE tags?
 
1 members found this post helpful.
Old 01-17-2011, 04:55 AM   #9
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
I am sorry ... I read that post .... disabled smilies from advanced options .... But I have no idea about CODE tags ...
 
Old 01-17-2011, 05:04 AM   #10
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,078

Rep: Reputation: 195Reputation: 195
Look for the # in the toolbar when you're composing a post.
 
1 members found this post helpful.
Old 01-17-2011, 06:14 AM   #11
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
ok .. I will do that ...
 
Old 01-17-2011, 10:08 AM   #12
ozanbaba
Member
 
Registered: May 2003
Location: Tengiz
Distribution: Slackware64 14.1
Posts: 672

Rep: Reputation: 94
I see what this profire do, now.
if user is root then /sbin /usr/sbin and /usr/local/sbin should searched first
if normal user then /sbin /usr/sbin and /usr/local/sbin should searched after.
 
1 members found this post helpful.
Old 01-17-2011, 11:07 AM   #13
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Hi ozanbaba

Thanks for your reply. But could you tell me why this difference in search list for a root and a normal user.
 
Old 01-17-2011, 11:23 AM   #14
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Quote:
Originally Posted by Soji Antony
Code:
# Path manipulation
if [ "$EUID" = "0" ]; then
    pathmunge /sbin
    pathmunge /usr/sbin
    pathmunge /usr/local/sbin
else
    pathmunge /usr/local/sbin after
    pathmunge /usr/sbin after
    pathmunge /sbin after
fi
Yes, the code above will behave differently whether the user is root or a normal user.

Directories named "sbin" normally contain system binaries/administration tools. For example: fdisk, fsck, ifconfig, chroot, usermod, etc. Most of those tools require root privileges at some point. The binaries in directories named "bin" generally do not require root privileges.

There are exceptions: some sbin binaries are useful without root privileges (ifconfig), and there are some bin binaries that can require root privileges. But what I said earlier is a good rule of thumb.

So, your /etc/profile arranges the PATH environment variable based on the expected behavior of the user. If the effective user ID is 0, then it's assumed that some system administration is necessary. So sbin directories are searched first.

If the effective user ID is not 0, then the system administration tools are placed at the end of the PATH because the user probably does not have sufficient rights to use them.

This arrangement follows with the general idea that a computer user should not run the system as root all the time--that root should be used only when necessary.
 
1 members found this post helpful.
Old 01-18-2011, 04:29 AM   #15
Soji Antony
Member
 
Registered: Jul 2010
Posts: 54

Original Poster
Rep: Reputation: 1
Dear Dark_Helmet
Many thanks for clearing my doubt ....
 
  


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
Need help about bash profile file. gardenair Linux - Newbie 6 03-05-2010 08:50 AM
user's profile file alaios Fedora 0 07-12-2004 07:57 PM
Why does /etc/profile take precedence over my .bashrc file? MDesigner Linux - Newbie 8 06-29-2004 09:55 PM
Problem with .profile file gallet Linux - Security 1 10-14-2003 04:49 PM
Profile file for redhat? KaktusKnight Linux - General 3 06-19-2003 01:17 AM


All times are GMT -5. The time now is 11:55 AM.

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