Re: interactive shell detection in shrc

Steffen Nurpmeso <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <20240923225640.BZkYDiMv@steffen%sdaoden.eu>
Valery Ushakov wrote in
 <[email protected]>:
 |On Tue, Sep 24, 2024 at 00:06:22 +0700, Robert Elz wrote:
 |
 |>     Date:        Mon, 23 Sep 2024 12:16:10 +0300
 |>     From:        Valery Ushakov <[email protected]>
 |>     Message-ID:  <[email protected]>
 |> 
 |>| I'd like to switch this to the "early return" style, something like
 |>|
 |>| # the rest of the file is for interactive shells only
 |>| case "$-" in *i*) ;; *) return ;; esac
 |> 
 |> I think the primary motivation for not doing it that way is so
 |> the file can be used as a script ("sh .shrc") for testing.
 |
 |That's kinda moot.  shrc is there to affect the state of a shell in a
 |way that is visible in subsequent use of that same shell.  sh .shrc
 |allows you to check that .shrc has no failures, but you cannot test
 |how it affected the shell (as opposed to sourcing it).  So if the
 |purpose of the sh .shrc excercise is just to check that .shrc can be
 |processed without failure one may almost unironically argue that the
 |return approach is actually better, b/c it will check the
 |interactive-only part too: sh .shrc is a _non_ interactive shell and
 |return in the non-interacive branch is ingored, so shell falls through
 |to the interactive-only part.
 |
 |I would say win-win :)

fwiw i have (in ~/.profile)

  # Some (ksh(1), NetBSD sh(1)) correctly auto-include $ENV, but bash(1) doesn't
  # (unless invoked interactively as sh(1) not bash(1));
  # also, Heirloom sh(1) doesn't know about it at all, so do include it here and
  # let it protect itself against multiple inclusion in the same shell
  ENV="${HOME}/.shrc"
  export ENV
  if [ -f "${ENV}" ]; then
          # Also, for bash(1) to recognize this $ENV when started as $bash not $sh,
          # we need to create a ~/.bashrc symlink!
          if [ -f ./.bashrc ]; then
                  :
          elif [ -L ./.bashrc ] && [ "$(readlink ./.bashrc)" = "${ENV}" ]; then
                  :
          else
                  rm -f ./.bashrc
                  ln -s "${ENV}" ./.bashrc
          fi
          # However, if we know for sure anything is fine, simply skip inclusion
          case ${0} in
          *ksh*|*bosh*)
                  ;;
          *)
                  . "${ENV}"
                  ;;
          esac
  fi

and then (in ~/.shrc)

  eval "___isinc=\$___SHRC$$"
  if [ -z "${___isinc}" ]; then
          eval "___SHRC${$}=YES"
          export ___SHRC${$}
          case ${-} in
          *i*|*m*) # {{{
  ...
          esac
  fi
  unset ___isinc

Loose-loose then. :(

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.