LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   /etc/profile script (https://www.linuxquestions.org/questions/linux-newbie-8/etc-profile-script-857736/)

Soji Antony 01-21-2011 03:50 AM

/etc/profile script
 
Quote:

This script is from /etc/profile file

Code:

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "$PS1" ]; then
            . $i
        else
            . $i >/dev/null 2>&1
        fi
    fi
done

Can any body tell me what the above script does ?

1. if [ "$PS1" ]; ??

When will this if condition become true ?

2. When this if condition is satisfied
* execute . $1
* else execute . $i >/dev/null 2>&1

I know $i >/dev/null 2>&1 will be executing silently .

But my question is why we need to execute $1 in two ways when that if condition is satisfied ??

i92guboj 01-21-2011 04:03 AM

Quote:

1. if [ "$PS1" ]; ??
This is an -unreliable- way to find whether the shell is interactive or not. There are better ways, but that's out of the scope of the thread, you can read more if you are interested in the issue here:

http://www.google.com/search?ie=UTF-...nteractive+ps1

Quote:

When will this if condition become true ?
The purpose is that it will be true when the shell is interactive (i.e. when you open a terminal or login into a tty). The reality is that it will be true when PS! is set (when there's a prompt). But, you can set the prompt to an empty string on an interactive shell. Hence, this is unreliable.

Quote:

2. When this if condition is satisfied
* execute . $1
* else execute . $i >/dev/null 2>&1
The "dot" command is equivalent to the "source" command. It dumps the script into the current shell instead of running it into a new instance.

Quote:

I know $i >/dev/null 2>&1 will be executing silently .

But my question is why we need to execute $1 in two ways when that if condition is satisfied ??
The intention is to let the messages be printed when the shell is interactive, but to ignore them when the script is ran on a non-interactive shell, during init or whatever.

Soji Antony 01-21-2011 04:16 AM

Thank you so much for your help!


All times are GMT -5. The time now is 06:35 AM.