mainautogen - Support: sr #111041, Dependency on non-portable and...

 
 

sr #111041: Dependency on non-portable and unreliable "which" command

Submitter:  Eli Schwartz <eschwartz>
Submitted:  Wed 27 Mar 2024 03:21:33 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  In Progress
Privacy:  Public Assigned to:  bkorb
Open/Closed:  Open Operating System:  None
* Mandatory Fields

Post a Comment

Add a New Comment Rich Markup
   

Discussion

Fri 03 Oct 2025 12:50:26 AM UTC, comment #2: 

Thanks for looking into this.

Note that as I said,

"However since this is a macro collection for autoconf it seems more apropos to use AC_CHECK_PROG or AC_CHECK_PROGS instead..."

Certainly for code that is inlined directly into a configure script there's no reason to use or have ever at any historic time used "which" even for code written before "command -v" existed. The autoconf macros for looking up a program in PATH exist quite specifically to portably solve the problem that "command -v" did not exist at the time that the macros were written.

The autoconf macros actually worked, work, and will always continue to work. If you have code that needs to run in a shell script outside of a configure script, then of course the portable way to implement a check for programs in PATH would be to copy paste the autoconf macro implementation into the standalone shell script. :)

Eli Schwartz <eschwartz>
Thu 02 Oct 2025 07:51:53 PM UTC, comment #1: 

There are a bunch of places where "which" was used.
Fixed in my sources, GIT repo awaits my access.
"command -v" was added long after this project was underway :)

Bruce Korb <bkorb>
Group administrator
Wed 27 Mar 2024 03:21:33 AM UTC, original submission:  

I noticed this when trying to build sharutils, which has libopts.m4 that ultimately comes from autogen.

  • QA Notice: command not found:
  • ./configure: 1: which: not found
  • ./configure: 1: which: not found




The code looks like this:

  [while :
  do
      test -x "$POSIX_SHELL" && break
      POSIX_SHELL=`which bash`
      test -x "$POSIX_SHELL" && break
      POSIX_SHELL=`which dash`
      test -x "$POSIX_SHELL" && break
      POSIX_SHELL=/usr/xpg4/bin/sh
      test -x "$POSIX_SHELL" && break
      POSIX_SHELL=`/bin/sh -c '
          exec 2>/dev/null
          if ! true ; then exit 1 ; fi
          echo /bin/sh'`
      test -x "$POSIX_SHELL" && break
      ]AC_MSG_ERROR([cannot locate a working POSIX shell])[
  done]
  AC_DEFINE_UNQUOTED([POSIX_SHELL], ["${POSIX_SHELL}"],
           [define to a working POSIX compliant shell])



Unfortunately, the use of "which bash" to check whether a bash program exists fails when the "which" command doesn't exist. See e.g.

https://unix.stac ... -then/85250#85250
https://mywiki.wo ... e.org/BashFAQ/081

In general, you can and should use "command -v" for this. It was part of POSIX 2008. A simple "s/which/command -v/" suffices.

However since this is a macro collection for autoconf it seems more apropos to use AC_CHECK_PROG or AC_CHECK_PROGS instead...

Eli Schwartz <eschwartz>

 

Attached Files

This item currently has no attached files.

(Note: upload size limit is set to 4.0MiB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

 

Dependencies

This item does not depend on any other items.

No items depend on this one.

 

Mail Notification Carbon-Copy List

Carbon-Copy List
  • -email is unavailable- added by bkorb (Posted a comment)
  • -email is unavailable- added by eschwartz (Submitted the item)
  •  

    Votes

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Please enter the title of George Orwell's famous dystopian book (it's a date):

    History

    Follow 2 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2025-10-02 bkorb Assigned toNone bkorb
    2025-10-02 bkorb StatusNone In Progress

    Back to the top

    Powered by Savane 3.16-598c.
    Corresponding source code