On Wed, 2021-06-09 at 04:56 +0200, Christoph Anton Mitterer wrote:
>    - Shouddn't it also cover the sourcing of /etc/profile.d/*.sh
>      (for the same reasons)?

Taking that one back,... /etc/profile.d/*.sh is obviously meant to me
like /etc/profile, which would also get sourced when invoked as `sh`.



I myself use now the following for /etc/profile and .profile (just with
/etc/bash.bashrc replaced with ~/.bashrc:
-----------------------------------------------------------------------
#check whether any internal variables used in this script have already been set 
and unset them
if [ -n "${profile_p+is_set}" ]; then
        printf 'Warning: The variable `profile_p` had already been set when 
executing `%s` and will be unset/overwritten.\n' '~/.profile' >&2
        unset -v profile_p
fi


#source `/etc/bash.bashrc`
if [ -z "${-##*i*}" ]  &&  [ -n "$-" ]; then
        #when executed in an interactive (login) shell …
        
        
        # The existence of a non-empty variable `BASH` is merely an indicator 
that the
        # shell might be bash. This serves as a rapid test.
        # However, its non-existence guarantees that the shell isn’t bash.
        
        if  [ -n "${BASH-}" ]   &&   { [ -n "${0##*/sh}" ]  &&  [ "$0" != 'sh' 
]; };  then
                #… that is (presumably) bash and not invoked as `sh` and …
                
                
                # `ps`, which is part of the non-essential package `procps`, 
may not be
                # available. `realpath`, which is part of the essential package 
`coreutils`, is
                # guaranteed to be available.
                
                if {  [ -x /bin/ps ]   &&   { profile_p="$( /bin/ps -o exe= -p 
$$ 2>/dev/null )" || profile_p='';  [ "${profile_p}" = /usr/bin/bash ]  ||  [ 
"${profile_p}" = /bin/bash ]; };  }    || \
                   {  [ -L /proc/$$/exe ]   &&   { profile_p="$( 
/usr/bin/realpath --canonicalize-existing /proc/$$/exe 2>/dev/null )" || 
profile_p='';  [ "${profile_p}" = /usr/bin/bash ]  ||  [ "${profile_p}" = 
/bin/bash ]; };  }; then
                        #… it’s actually bash
                        
                        
                        if [ -f /etc/bash.bashrc ]; then
                                . /etc/bash.bashrc
                        fi
                fi
        fi
fi


#source `/etc/profile.d/*.sh`                                         
if [ -d /etc/profile.d ]; then
        for profile_p in /etc/profile.d/*.sh; do
                if [ -f "${profile_p}" ]; then
                        . "${profile_p}"
                fi
        done
fi


#cleanups
unset -v profile_p
-----------------------------------------------------------------------

That also no longer sets PS1 as /etc/profile does right now. This would
anyway just get used in login shells and just for non-bash... and it
seems e.g. dash sets #/$ automatically.

But of course one could just add the `else` back above.


It also uses -f instead of -r when sourcing /etc/profile.d . I think it
makes more sense to check whether these are regular files and actually
give and error if they're not readable.


Maybe one can just drop the {...} that uses ps to find out which binary
is used by the shell - ps also needs /proc.
Also, this might not work for HURD/kFreeBSD ... so either just not
check it at all, or one might need to add some alternative way for
those.



Well, pick whatever you like :-D


Cheers,
Chris.

Reply via email to