Bug#289732: Conditional command execution

2005-01-18 Thread Thomas Hood
> So I guess you need to do this:

Actually, the snippet I gave can be simplified since "test -x" simply
returns false if it is given an empty string as argument.

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

-- 
Thomas Hood <[EMAIL PROTECTED]>



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]



Bug#289732: Conditional command execution

2005-01-18 Thread Thomas Hood
(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  
>  else
>  /etc/init.d/ 
>  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 <[EMAIL PROTECTED]>



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]