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

Bug#218530: Conditional command execution



(cc:ed to #218530)

Guilherme de S. Pastore wrote to #289732:
> The attached patches make your maintainer scripts behave just like a
> package using debhelper would


The patch makes use of "which".  The problem with "which" is supposed to
be that it is in /usr/bin/ and (as Ian Jackson wrote in #218530):

> `which' is wrong, because we want to know how the shell which is
> actually executing the maintainer script will execute the command.

Andreas Metzler wrote later in #218530:

> FWIW posix suggests to use "command -v" if available, "The command -v
> and -V options were added to satisfy requirements from users that are
> currently accomplished by three different historical utilities: type
> in the System V shell, whence in the KornShell, and which in the C
> shell.[...]"

So I take it that "command -v" is actually recommended.  The problems
(judging from the original submission) are, first, that it is being used
in a #!/bin/sh script, whereas "command -v" isn't implemented by all
POSIX shells (see policy 10.4), and second, that the command isn't being
checked for executability.

What is the best way to run a command if and only if it is on the PATH?

Debian policy section 9.3.3.2 was later changed by Manoj to show how to
test for the presence of a command using "command -v".


>          if command -v invoke-rc.d >/dev/null 2>&1; then
>              invoke-rc.d <package> <action>
>          else
>              /etc/init.d/<package> <action>
>          fi


However, this does not check that the invoke-rc.d file found on the
patch can actually be executed.  So I guess you need to do this:

    #!/bin/bash
    ...
    if CMD="$(command -v update-menus 2>/dev/null)" && [ "$CMD" ] && [ -x "$CMD" ] ; then
        update-menus
    fi  

-- 
Thomas Hood <jdthood@aglu.demon.nl>




Reply to: