[ubuntu-za] Script error

Alf Stockton alf at stockton.co.za
Tue Apr 14 13:40:23 BST 2009


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


-- 
Regards,
Alf Stockton		www.stockton.co.za

You can create your own opportunities this week.  Blackmail a senior executive.



More information about the ubuntu-za mailing list