Subject: bug#73402: closed (Re: bug#73402: Switch scripts from using `type` to `command -v` to support non-XSI POSIX shells, or specify bash?)

From: [email-masked] (GNU bug Tracking System)
Date: Sun, 22 Sep 2024 07:10:02 +0000

Your bug report

#73402: Switch scripts from using `type` to `command -v` to support non-XSI POSIX shells, or specify bash?

which was filed against the gzip package, has been closed.

The explanation is attached below, along with your original report.
If you require more details, please reply to [email-masked].

-- 
73402: https://debbugs.gnu.org/cgi/bugreport.cgi?bug=73402
GNU Bug Tracking System
Contact [email-masked] with problems

Subject: Re: bug#73402: Switch scripts from using `type` to `command -v` to support non-XSI POSIX shells, or specify bash?
From: Paul Eggert <[email-masked]>
To: Sparr <[email-masked]>
Cc: [email-masked]
Date: Sun, 22 Sep 2024 00:09:24 -0700
List-Id: <debbugs-submit.debbugs.gnu.org>

Thanks for the bug report. I installed the attached patch to fix the bug 
and am boldly closing the bug report. If the patch doesn't work for you, 
please let us know and we can reopen the bug report.

From 602fa20438cddaa8ede84828b6fbe42a06d49392 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Sun, 22 Sep 2024 00:06:44 -0700
Subject: [PATCH] =?UTF-8?q?maint:=20=E2=80=98type=E2=80=99=20=E2=86=92=20?=
 =?UTF-8?q?=E2=80=98command=20-v=E2=80=99?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

POSIX says ‘type’ is an XSI extension, whereas ‘command -v’ is
part of the base.  Problem reported by Clarence “Sparr” Risher in
<https://bugs.gnu.org/73402>.
* gzexe.in, tests/help-version, zdiff.in, zgrep.in:
Use ‘command -v’, not ‘type’.
---
 gzexe.in           | 4 ++--
 tests/help-version | 2 +-
 zdiff.in           | 2 +-
 zgrep.in           | 2 +-
 4 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/gzexe.in b/gzexe.in
index 4504d77..217381d 100644
--- a/gzexe.in
+++ b/gzexe.in
@@ -116,7 +116,7 @@ for i do
   test -d "$dir" && test -w "$dir" && test -x "$dir" || dir=/tmp
   test -n "$tmp" && rm -f "$tmp"
   if test -z "$mktemp_status"; then
-    type mktemp >/dev/null 2>&1
+    command -v mktemp >/dev/null 2>&1
     mktemp_status=$?
   fi
   case $dir in
@@ -166,7 +166,7 @@ case $TMPDIR in
   /*) TMPDIR=$TMPDIR/;;
   *) TMPDIR=/tmp/;;
 esac
-if type mktemp >/dev/null 2>&1; then
+if command -v mktemp >/dev/null 2>&1; then
   gztmpdir=`mktemp -d "${TMPDIR}gztmpXXXXXXXXX"`
 else
   gztmpdir=${TMPDIR}gztmp$$; mkdir $gztmpdir
diff --git a/tests/help-version b/tests/help-version
index 809d958..3b2fe03 100755
--- a/tests/help-version
+++ b/tests/help-version
@@ -255,7 +255,7 @@ for i in $built_programs; do
   mkdir $tmp_dir
   # echo ================== $i
   test $i = [ && prog=lbracket || prog=$(echo $i|sed "s/$EXEEXT$//")
-  if type ${prog}_setup > /dev/null 2>&1; then
+  if command -v ${prog}_setup > /dev/null 2>&1; then
     ${prog}_setup
   else
     args=
diff --git a/zdiff.in b/zdiff.in
index a76e91b..38f83db 100644
--- a/zdiff.in
+++ b/zdiff.in
@@ -155,7 +155,7 @@ case $file2 in
                           /*) TMPDIR=$TMPDIR/;;
                           *) TMPDIR=/tmp/;;
                         esac
-                        if type mktemp >/dev/null 2>&1; then
+                        if command -v mktemp >/dev/null 2>&1; then
                           tmp=`mktemp "${TMPDIR}zdiffXXXXXXXXX"` ||
                             exit 2
                         else
diff --git a/zgrep.in b/zgrep.in
index ab80d56..acb5168 100644
--- a/zgrep.in
+++ b/zgrep.in
@@ -142,7 +142,7 @@ while test $# -ne 0; do
         /*) TMPDIR=$TMPDIR/;;
         *) TMPDIR=/tmp/;;
       esac
-      if type mktemp >/dev/null 2>&1; then
+      if command -v mktemp >/dev/null 2>&1; then
         pattmp=$(mktemp "${TMPDIR}zgrepXXXXXXXXX") || exit 2
       else
         set -C
-- 
2.43.0

Subject: Switch scripts from using `type` to `command -v` to support non-XSI POSIX shells, or specify bash?
From: Sparr <[email-masked]>
To: [email-masked]
Date: Fri, 20 Sep 2024 19:10:53 -0400
List-Id: <debbugs-submit.debbugs.gnu.org>

The `zdiff` and `gzexe` scripts use the `type` command to identify whether
the `mktemp` tool is available. That command exists in XSI-compliant shells
and as an external binary or script in some operating systems, but it is
missing in simple POSIX-compliant shells. Because those scripts start with
`#!/bin/sh` I believe they should work in POSIX-compliant shells. Replacing
the use of `type` with `command -v`, or a more complex construct that uses
`type` when it is available and falls back to `command -v` otherwise, would
allow the scripts to work as intended in those scenarios, rather than
silently failing to find the `mktemp` tool when it is available.

Alternately, if the intent is to only target bash, I would propose the
shebang be changed, although that could have downstream consequences of
introducing a new dependency on systems that don't otherwise need or use
bash.

I apologize if this has been discussed previously. I was not able to find
it in the mailing list archives, but I am also unfamiliar with the
organization of the GNU projects' communication and development channels. I
am sending this email without subscribing to the list because that seems to
be the suggested approach in the docs; please let me know if there is a
better way to address this concern.

--
Sparr
[email-masked]
http://github.com/sparr