Re: interactive shell detection in shrc

Robert Elz <[email protected]>
Newsgroups gmane.os.netbsd.devel.userlevel
Message-ID <[email protected]>
    Date:        Tue, 24 Sep 2024 00:56:40 +0200
    From:        Steffen Nurpmeso <[email protected]>
    Message-ID:  <20240923225640.BZkYDiMv@steffen%sdaoden.eu>

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

Random suggestions, preceded by a question?

Why are you exporting ___SHRC$$ ?    As long as this shell
continues running, there cannot be another process with the
same $$ so the include guard cannot possibly have any effect
anywhere else, and this shell doesn't need the variable to
be exported to see it.   By the time $$ is the same value in
some other shell, you want the .shrc processed, don't you?

Given that it doesn't need exporting, it also doesn't need
the $$ in its name either, so

	if $___SHRC
	then
		___SHRC=false
		case $- in
		*i*|*m*)
			# whatever
			;;
		*)
			# whatever else
			;;
		esac
		# common stuff for all cases
	fi

If you insist on quoting the expansion in the first line,
it needs to become:

	if "${___SHRC:-:}"

even if you want too keep the $$ and export for some reason

	if eval "\$___SHRC$$"
	then
		eval "___SHRC$$=false"
		export "___SHRC$$"
		# etc
	fi

with the similar variation if you want the ___SHRC$$ expansion
quoted:
	if eval "\"\${___SHRC$$:-:}\""

kre

ps: writing $$ (or any of the special params) as ${$} (etc)
is really taking things a little far, unless you want a more
complex expansion, like ${!:-$$} or something.   It is even
weirder when you write $$ in one place and ${$} in others
(and use $- rather than ${-}).   And of course using $# rather
than ${#} makes it clear that you want the number of set
positional params, and not the length of something that you
forgot to insert...
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.