[pacman-dev] [PATCH] scripts: replace test builtin [ with shell keywords [[ and ((

Dan McGee dpmcgee at gmail.com
Tue Nov 10 15:23:25 EST 2009


On Tue, Nov 10, 2009 at 2:20 PM, Cedric Staniewski <cedric at gmx.ca> wrote:
> Isaac Good wrote:
>> On Fri, Nov 06, 2009 at 08:01:04PM +0000, Cedric Staniewski wrote:
>>> Forgot to point out that the 'type -p anything' tests might not work as intended.
>>>
>>>> -           if [ ! "$(type -p xdelta3)" ]; then
>>>> +           if ! type xdelta3 &>/dev/null; then
>>>
>>> $ type asdf
>>> bash: type: asdf: not found
>>> $ type -p asdf; echo $?
>>> 1
>>> $
>>> $
>>> $ touch ~/bin/asdf; chmod +x ~/bin/asdf
>>> $ type asdf
>>> asdf is /home/makepkg/bin/asdf
>>> $ type -p asdf; echo $?
>>> /home/makepkg/bin/asdf
>>> 0
>>> $ rm ~/bin/asdf
>>> $
>>> $
>>> $ asdf() {
>>>> echo 1
>>>> }
>>> $ type asdf
>>> asdf is a function
>>> asdf ()
>>> {
>>>     echo 1
>>> }
>>> $ type -p asdf; echo $?
>>> 0
>>>
>>>
>>> That means you cannot rely on the return code when you want to emulate which.
>>> You have to test the length of the resulting string instead.
>>>
>>
>> If the command is a bash function, is that not good enough? (ie why the -p)
>> type -pf will suppress functions, so you can do : if type -pf xdelta3 ; then
>>
>> $  type -p q ; echo $? ; q () { echo ; } ; type -pf q ; echo $? ; type -p q ; echo $?
>> 1
>> 1
>> 0
>>
>> Definitely worth its own patch, though.
>>
>> - Isaac
>
> It might be enough, but I wonder, as well, why the -p option is used. I removed it in my patch since it is not needed when testing the return code, but maybe I missed something.

dmcgee at galway ~/projects/pacman (gpg-more)
$ type -p xdelta3; echo $?
1

dmcgee at galway ~/projects/pacman (gpg-more)
$ type xdelta3; echo $?
-bash: type: xdelta3: not found
1

Note that -p suppresses error messages that you may not want; it's
output is only the return code.

-Dan


More information about the pacman-dev mailing list