[ubuntu-za] Script error
alf at stockton.co.za
Tue Apr 14 13:52:52 BST 2009
Alf Stockton wrote:
> Jonathan Hitchcock wrote:
>> On 14 Apr 2009, at 2:20 PM, Alf Stockton wrote:
>>> if [ -f /mnt/backup/mailbackups/*.dotproject.files.tar.gz ]
>>> rm /mnt/backup/mailbackups/*.dotproject.files.tar.gz
>>> 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:
>> 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 ]
> rm /mnt/backup/mailbackups/*.dotproject.backup.sql
To answer my own question. The above would work if there was only one file
matching the *.dotproject.backup.sql.
Alf Stockton www.stockton.co.za
Chicken Little only has to be right once.
More information about the ubuntu-za