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 02-27-2010, 11:13 AM   #1
carlc1
LQ Newbie
 
Registered: Feb 2010
Posts: 12

Rep: Reputation: 0
/etc/profile logic question


I'm trying to decipher the logic of the following section of code in /ect/profile (it's for an assignment in a class I'm taking):

Code:
test -r /etc/bash.bashrc  && . /etc/bash.bashrc
if test "$is" = "bash" -a -z "$_HOMEBASHRC" ; then
   # loop detection
   readonly _HOMEBASHRC=true
   test -r $HOME/ .bashrc  && . $HOME/ .bashrc
fi
Here's my take as far as I can I can figure out, first it tests whether or not the /etc/bash.bashrc file exists and is readable, and if so it then reads and executes the commands in said file.

In the second line I obviously know that it's testing if the shell is bash, but what's with the -a -z switches? And is it also testing if there is a _HOMEBASHRC variable (or if it's empty) as well?

So, if line two is true, then it gives _HOMEBSHRC a value of true, and makes it so that value cannot be changed by subsequent assignment. It also then tests if .bashrc in the home directory exits and is readable, and if so it then reads and executed the commands in that file.

LOL, so am I close here or way off base? Any help would be greatly appreciated.
 
Old 02-27-2010, 11:44 AM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
In the first line, it tests as you say, and then--if successful, it sources the file, meaning that it makes it part of the script. What happens next depends on what's in that file.

Next, I assume that the variable "is" is defined somewhere (including in .bashrc---which was just sourced).

-a is used as "and", and -z means test for a zero-length string. This means it wants to know if that variable exists and is empty???

I can't analyze any more without my Excedrin---but it seems that you are on track. BTW, you get an A+ for how to ask a homework question.
 
Old 02-27-2010, 02:06 PM   #3
carlc1
LQ Newbie
 
Registered: Feb 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Thanks a bunch! So, if I understand correctly, the code does this:

Tests if /etc/bash.bashrc exists and is readable, and if so it sources the file, making it part of the script. Then if the value of the is variable equals "bash" and the value of the _HOMEBASHRC variable is a zero length string, it assigns a value of "true" to the _HOMEBASHRC variable, tests if .bashrc in the user's home directory exists and is readable, and if so it sources that file to the script as well.

So basically, it was not knowing the syntax for sourcing a file, and not havine a clue what the -a and -z options were. Cool. Thanks again.
 
Old 02-28-2010, 03:05 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by carlc1 View Post
Tests if /etc/bash.bashrc exists and is readable, and if so it sources the file, making it part of the script. Then if the value of the is variable equals "bash" and the value of the _HOMEBASHRC variable is a zero length string, it assigns a value of "true" to the _HOMEBASHRC variable, tests if .bashrc in the user's home directory exists and is readable, and if so it sources that file to the script as well.
OK, that's what it is doing -- but why? Is it defending against the case where ~/.bashrc sources /etc/profile and /etc/profile sources ~/.bashrc? If so, why only do it when $is (huh? Where did that come from?) contains bash?

BTW there are spaces (marked with an X below) where there should not be
Code:
test -r $HOME/X.bashrc  && . $HOME/X.bashrc
 
Old 02-28-2010, 09:29 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by carlc1 View Post
So basically, it was not knowing the syntax for sourcing a file, and not havine a clue what the -a and -z options were. Cool. Thanks again.
"man test" for those options and more.

For things like source, go to http://tldp.org and get:
Bash Guide for Beginners
Advanced Bash Scripting Guide
 
Old 03-02-2010, 04:04 PM   #6
carlc1
LQ Newbie
 
Registered: Feb 2010
Posts: 12

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by catkin View Post
OK, that's what it is doing -- but why? Is it defending against the case where ~/.bashrc sources /etc/profile and /etc/profile sources ~/.bashrc? If so, why only do it when $is (huh? Where did that come from?) contains bash?
That's what I'm trying to figure out on my own. Now that I know what the code is doing, I might have a chance at figuring out what it is accomplishing, and why. As soon as I think I have an idea, I'll post and see if I'm close.
 
Old 03-02-2010, 04:36 PM   #7
carlc1
LQ Newbie
 
Registered: Feb 2010
Posts: 12

Original Poster
Rep: Reputation: 0
OK, I think that /etc/profile is a login shell script. So, when a user logs in, /etc/profile is run and sets up the environment for the user's login shell. Is this correct? I'm now thinking that /etc/bash.bashrc is run when non-login shells are started, but I'm not sure.
 
Old 03-02-2010, 07:49 PM   #8
carlc1
LQ Newbie
 
Registered: Feb 2010
Posts: 12

Original Poster
Rep: Reputation: 0
OK, I think I have it now. The logic in that section simply executes the code in the .bashrc file so that all the setting there also take effect in a login shell. Is that correct?
 
Old 03-02-2010, 10:58 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by carlc1 View Post
OK, I think I have it now. The logic in that section simply executes the code in the .bashrc file so that all the setting there also take effect in a login shell. Is that correct?
/etc/profile is run (sourced) by several shells on login. When a non-login interactive bash shell starts it sources .bashrc. The full dirt here.

Many non-login interactive bash shells run in terminal emulators, started from a GUI desktop. The net effect is that bash shells can be confusingly different depending on whether they are started at a login prompt or from the desktop. One solution -- for greater similarity -- is to source .bashrc from within /etc/profile. Mostly /etc/profile sets environment variables and these are inherited by every process that is a child of the login shell. The first step of logging in to a GUI desktop is to run a login shell thus all the processes comprising the desktop inherit /etc/profile's environment variables, including any interactive bash shells running in terminal emulators.

That's the basic mechanism that the code you posted addresses but it is normally done very simply by including something like this in /etc/profile
Code:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
I suspect that the extra complexity of the code you posted is to defend against an infinite loop in the case where .bashrc also sources /ec/profile.
 
Old 03-02-2010, 11:14 PM   #10
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I think you are reading an /etc/profile script for openSuSE Linux.
The $is variable is defined earlier in the file:
Code:
#
# Check which shell is reading this file
#
if test -f /proc/mounts ; then
  case "`/bin/ls -l /proc/$$/exe`" in
    */bash)     is=bash
        read -t 1 a r </proc/$$/cmdline
        case "$a" in
        sh|-sh|*/sh)
                is=sh   ;;
        esac            ;;
    */ash)      is=ash  ;;
    */ksh)      is=ksh  ;;
    */pdksh)    is=ksh  ;;
    */zsh)      is=zsh  ;;
    */*)        is=sh   ;;
  esac
Also look in /etc/profile.d/. Packages that need to change your PATH variable, add library paths or define variables in your environment will drop scripts in this directory rather than edit the /etc/profile script. The directory contains scripts for bash and csh. Which ones are sourced depends on your default shell.
 
  


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
Question about /etc/profile.d directory coldbeer Slackware 1 09-08-2009 03:05 PM
LSI Logic / Symbios Logic 53c875 (rev 14) -> HP Storageworks 1/8 G2 gileravxr Linux - Hardware 0 07-21-2009 04:45 AM
Hardware Profile Question JoeOnTheGo Linux - Newbie 2 12-11-2007 02:25 PM
$PATH /etc/profile question ewlabonte Debian 7 07-03-2007 02:57 PM
quick mysql question - logic with COUNT BrianK Programming 3 09-29-2005 04:29 PM

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

All times are GMT -5. The time now is 06:40 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
Open Source Consulting | Domain Registration