bug-gawk
[Top][All Lists]
Advanced

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

Re: no exit status available when a command doesn't exist or fails in pi


From: Nelson H. F. Beebe
Subject: Re: no exit status available when a command doesn't exist or fails in pipe to grep
Date: Mon, 6 Sep 2021 13:21:03 -0600

Ed Morton asks about testing for the existence of a program in awk or
in the shell:

>> I don't suppose you know of a POSIX equivalent?

What about this:

        % which garbage
        garbage: Command not found.

        % \which --version
        GNU which v2.21, Copyright (C) 1999 - 2015 Carlo Wood.
        ...

        % \which garbage
        which: no garbage in (... PATH expansion here ...)

"which" is a built-in in some shells; I routinely install the GNU
version on new systems in our build farm.  The standalone version has
a useful -a (all) option, but the built-in does not:

        % which -a gcc
        -a: Command not found.
        /usr/bin/gcc

        % \which -a gcc
        /usr/bin/gcc
        /bin/gcc

However, in Debian 11 updates, I recently got this report:

        /usr/bin/which: this version of `which' is deprecated;
        use `command -v' in scripts instead.

Here is an example of its use:

        % /usr/bin/command -v garbage || echo MISSING garbage
        MISSING garbage

I find "command" in DragonFlyBSD, FreeBSD, most Linux, and Solaris
family, but it is not available in recent GnuiOS, Gnewsense,
GoboLinux, GNU/Hurd, NetBSD and OpenBSD systems.

The pathfind tool in our book, Classic Shell Scripting, is a
generalization of "which" to search arbitrary user-supplied paths:

        % pathfind PATH gcc
        /usr/bin/gcc

        % pathfind -a PATH gcc
        /usr/bin/gcc
        /bin/gcc

You can get it from

        http://www.math.utah.edu/pub/pathfind

It installs easily anywhere, but isn't a standard Unix tool.

-------------------------------------------------------------------------------
- Nelson H. F. Beebe                    Tel: +1 801 581 5254                  -
- University of Utah                    FAX: +1 801 581 4148                  -
- Department of Mathematics, 110 LCB    Internet e-mail: beebe@math.utah.edu  -
- 155 S 1400 E RM 233                       beebe@acm.org  beebe@computer.org -
- Salt Lake City, UT 84112-0090, USA    URL: http://www.math.utah.edu/~beebe/ -
-------------------------------------------------------------------------------



reply via email to

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