LinuxQuestions.org
Visit Jeremy's Blog.
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 07-18-2017, 07:42 AM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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.
 
Old 07-18-2017, 01:57 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
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

Last edited by MadeInGermany; 07-18-2017 at 02:15 PM.
 
1 members found this post helpful.
Old 07-18-2017, 06:27 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,789

Rep: Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217Reputation: 2217
/etc/profile needs to be compatible with all the shells that source it, not just bash.
 
Old 07-19-2017, 09:28 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
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.
 
Old 07-19-2017, 12:27 PM   #5
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
I think I'll have a lot of other questions, but first I'd like to know what flags you're referring to.
 
Old 07-19-2017, 01:23 PM   #6
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
The tradtional option flags that you can set or unset.
For example:
Code:
$ echo $-
himBH
$ set -f
$ echo $-
fhimBH
$ set +f
$ echo $-
himBH
 
Old 07-19-2017, 01:35 PM   #7
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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)?
 
Old 07-19-2017, 01:56 PM   #8
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,915

Rep: Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236Reputation: 1236
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
 
Old 07-19-2017, 02:50 PM   #9
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Original Poster
Rep: Reputation: 103Reputation: 103
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?
 
Old 07-19-2017, 04:38 PM   #10
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
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
 
  


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
grepping a sentence? willc86 Linux - Newbie 4 07-20-2015 12:12 PM
[SOLVED] How to import Thunderbird Profile backup containng mail archive with Profile Manager? Beukel Linux - Newbie 2 02-16-2014 03:01 AM
Understanding Profile file in Solaris 10 rahulchandrak Solaris / OpenSolaris 3 01-10-2011 12:36 AM
What is bad in this sentence? Coimbra Linux - Newbie 9 07-11-2007 02:15 PM
What does this sentence mean? shadkong Slackware 9 04-07-2005 06:42 AM

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

All times are GMT -5. The time now is 07:20 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