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

Cedric Staniewski cedric at gmx.ca
Tue Nov 10 15:20:02 EST 2009


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.


More information about the pacman-dev mailing list