[ubuntu-za] Script error

Walter Leibbrandt walter+ubuntuza at translate.org.za
Tue Apr 14 13:43:12 BST 2009


Alf Stockton wrote:
> Jonathan Hitchcock wrote:
>   
>> Hi,
>>
>> On 14 Apr 2009, at 2:20 PM, Alf Stockton wrote:
>>     
>>> #/bin/dash
>>> if [ -f /mnt/backup/mailbackups/*.dotproject.files.tar.gz ]
>>>     then
>>>     rm /mnt/backup/mailbackups/*.dotproject.files.tar.gz
>>> fi
>>>
>>> and I keep getting the error:-
>>>
>>> ./DingDong.sh: line 2: [: too many arguments
>>>       
>> When you have a * on a commandline, the shell replaces it with all the  
>> files that match that pattern.  So if you have files like the following:
>>
>> /mnt/backup/mailbackups/foo.dotproject.files.tar.gz
>> /mnt/backup/mailbackups/bar.dotproject.files.tar.gz
>>
>> Then the 'if' statement above will be expanded to the following:
>>
>> if [ -f /mnt/backup/mailbackups/foo.dotproject.files.tar.gz /mnt/ 
>> backup/mailbackups/bar.dotproject.files.tar.gz ]
>>
>> This happens before the 'if' statement is evaluated.  And, of course,  
>> the "-f" test does not take more than one filename as an argument,  
>> which is why you're getting the "too many arguments" error.
>>
>> Basically, you can't use "-f" to test for the existence of "any file  
>> matching this pattern".  If you wanted to do that, you would have to  
>> do something like evaluating the pattern, storing it in a variable,  
>> and testing the length of the variable, or something.
>>
>> But it seems that all you want to do is remove any files matching that  
>> pattern, if they exist.  If that is the case, then just replace the  
>> whole thing with:
>>
>> rm -f /mnt/backup/mailbackups/*.dotproject.files.tar.gz
>>
>> The '-f' means 'unconditionally', or, in other words "whether they  
>> exist or not".
>>
>>
>>     
> Then I am certainly confused as the following works fine in another script.
>
> if [ -f /mnt/backup/mailbackups/*.dotproject.backup.sql ]
>      then
>      rm /mnt/backup/mailbackups/*.dotproject.backup.sql
>      fi
That should not work if /mnt/backup/mailbackups/*.dotproject.backup.sql 
expands to more than one filename.

-- 
Walter Leibbrandt                  Software Developer
Recent blogs:
* Firefox-style button with a pop-up menu
http://www.translate.org.za/blogs/walter/en/content/firefox-style-button-pop-menu





More information about the ubuntu-za mailing list