LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   understanding conditional sentence in /etc/profile (https://www.linuxquestions.org/questions/linux-newbie-8/understanding-conditional-sentence-in-etc-profile-4175610096/)

vincix 07-18-2017 07:42 AM

understanding conditional sentence in /etc/profile
 
Hi,
I'd ilke to understand the meaning of the following script excerpt:
Code:

for i in /etc/profile.d/*.sh ; do
    if [ -r "$i" ]; then
        if [ "${-#*i}" != "$-" ]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi
    fi

I'm mostly interested in this part: [ "${-#*i}" != "$-" ]. How is the variable modified/interpreted here? I did read a good chunk of Advanced Bash (tldp.org), but I didn't see any mention of this. It reminds me about indirection/substring replacement.

MadeInGermany 07-18-2017 01:57 PM

For all *.sh scripts in /etc/profile.d/
If the current shell is interactive, source(include) the sh script,
otherwise source(include) the sh script but suppress output.

The test for an interactive shell looks a bit complex:
$- or ${-} is the current set of flags that one can turn on or off with the set command.
${-#*i} means it chops the first characters until (and including) an i from the $-
The i is the flag for being interactive.
If the chopping was successful the result is shorter than the $- and certainly not equal.

I would have coded this more simple
Code:

        if [[ $- == *i* ]]; then
            . "$i"
        else
            . "$i" >/dev/null
        fi

Or
Code:

        case $- in
        ( *i* )
            . "$i"
        ;;
        ( * )
            . "$i" >/dev/null
        ;;
        esac


rknichols 07-18-2017 06:27 PM

/etc/profile needs to be compatible with all the shells that source it, not just bash.

MadeInGermany 07-19-2017 09:28 AM

Yes, and my case-esac sample works with all sh-style shells (but the old SySV Bourne shell).
--
It's a general disease that people think light and code heavily. The brain moves into the fingers.

vincix 07-19-2017 12:27 PM

I think I'll have a lot of other questions, but first I'd like to know what flags you're referring to.

MadeInGermany 07-19-2017 01:23 PM

The tradtional option flags that you can set or unset.
For example:
Code:

$ echo $-
himBH
$ set -f
$ echo $-
fhimBH
$ set +f
$ echo $-
himBH


vincix 07-19-2017 01:35 PM

But these are different from the flags in this link, right? http://tldp.org/LDP/abs/html/options.html (with the exception of i and B)
Do you know any links where I can learn more of this or some other practical examples maybe?

set -f means that you're usetting f? If that's so, why does it show up when you do echo $-? And the f flag is the same as in the link (no globbing)?

MadeInGermany 07-19-2017 01:56 PM

The traditional option flags are the -X options in the left table column in your URL, where X are the given single letters.
They do not have an argument, that's why I call them flags.
In contrast to
Code:

set -o longoption
(where longoption is in table column 2): the -o ... is a bash extension.
Code:

set -X
turns an option on, + turns it off.
Code:

set -f
is indeed identical with
Code:

set -o noglob

vincix 07-19-2017 02:50 PM

The reason why I thought the flags in the table were different from that flags seen using echo $- was that m and h and H are missing for some reason. Why's that?:)

norobro 07-19-2017 04:38 PM

From your link:
Quote:

The following is a listing of some useful options.
For a comprehensive list see: https://www.gnu.org/software/bash/ma...he-Set-Builtin


All times are GMT -5. The time now is 09:26 PM.