grep is always recursive

Cameron Hutchison lists at xdna.net
Mon Feb 2 22:06:14 UTC 2009


Lorenzo Luengo <lluengo at dgeo.udec.cl> writes:

>Matthew Flaschen wrote:
>> Lorenzo Luengo wrote:
>>   
>>> I think it's also a great idea. And it does not break backwards 
>>> compatibility! Let's see our study case:
>>>
>>> $ ls
>>> somefile   -r somotherfile
>>>
>>> Now when you do
>>> $ grep someword *
>>>
>>> It would expand to
>>> $ grep someword somefile ./-r somotherfile
>>
>> It's not backwards-compatible.  Below is a sample script that breaks.
>> Granted, it's slightly contrived, but nonetheless a valid example.  To
>> test it, save as author_from_initials.sh, then do:
>>
>> chmod a+x author_from_initials.sh
>> mkdir author_test
>> touch author_test/JES_proposal.txt
>> cd author_test
>> ../author_from_initials.sh *
>>
>> It will print:
>>
>> JES_proposal.txt: John Edwards Smith
>>
>> If you do:
>>
>> ../author_from_initials.sh ./JES_proposal.txt
>>
>> (the latter is what your proposal entails), it will give:
>>
>> ./JES_proposal.txt: Author not found
>>
>> --------------------------------------------------
>>
>> #!/bin/sh
>> for filename in "$@"
>> do
>>     echo -n "$filename: "
>>     case $filename in
>> 	JES*)
>> 	    echo "John Edwards Smith"
>> 	    ;;
>> 	MJT*)
>> 	    echo "Mary Jane Thompson"
>> 	    ;;
>> 	*)
>> 	    echo "Author not found";
>> 	    ;;
>>     esac
>> done
>>
>>   
>But I think it doesn't work because the way you coded your case matching 
>like JES*, but the filename is still correct and valid filename, and not 
>an spurious option.

It doesn't work because it's a poor script that cannot handle filenames
with paths. The script would also fail with:

$ author_from_initials.sh ~/doc/*

But, more importantly, Matthew has misunderstood the proposal. Only
filenames starting with a dash are prefixed with a ./ . He missed the
part in your example where only -r was turned into ./-r. The other
filenames were expanded as expected.

Given the proposal, Matthew's script would continue to work as expected.





More information about the ubuntu-users mailing list