bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 0/4] Add import builtin


From: Robert Elz
Subject: Re: [PATCH 0/4] Add import builtin
Date: Sat, 04 May 2024 04:24:33 +0700

    Date:        Fri, 3 May 2024 09:18:09 -0400
    From:        Chet Ramey <chet.ramey@case.edu>
    Message-ID:  <842de6ec-c7f9-47fb-ab21-e25c0bfd85dc@case.edu>

  | How is this any different than
  |
  | PATH=$BASH_IMPORT_PATH:$PATH source
  | or
  | PATH=$BASH_IMPORT_PATH source
  |
  | You could force the latter if you are sure that the sourced files will
  | not attempt to run external commands.

Another safer way might be

        findfile()
        {
                local - P IFS=:

                case "$1" in
                */*)    printf %s "$1"; return;;
                esac

                set -f
                for P in ${BASH_IMPORT_PATH:-.}
                do
                        if test -f "${P:-.}/$1"
                        then
                                printf %s "${P:-.}/$1"
                                return
                        fi
                done
                printf %s "$1"
        }

And then

        . "$(findfile whatever)"

relying upon the error message from '.' if whatever isn't found (and
use whatever name you like instead of "findfile").

An alternative I hate to even mention, as it is so hacky (and has
mostly been deleted from most of its descendants) is the ash shell
%func PATH hack: if an entry in $PATH ends in %func and the search for a
command reaches that entry, and a file exists in the directory named
(with the "%func" deleted) with the name of the command being sought,
then the shell will (would) source that file, and if after doing that
a function with the name of the command being sought had appeared, then
run that function as the command.   Otherwise, just continue with the
PATH search (more than one %func entry was possible).


So I agree, "import" is definitely not needed, and if it were
really to be deemed useful functionality, "import" is the wrong name.

kre




reply via email to

[Prev in Thread] Current Thread [Next in Thread]