[vox-tech] Bash scripting newbie - need syntax help

ME vox-tech@lists.lugod.org
Wed, 28 Apr 2004 00:08:40 -0700 (PDT)


Larry Ozeran said:
> Hi all -
>
> The short version of my problem is I need help with a recursive bash
> script, and I have never written a bash script before. If you want to skip
> this lengthy intro, you may be able to tell what I am trying to do from
> looking at my script attempt (labeled below).
>
> * * * * * * Lengthy intro
>
> I had noticed that logrotate was running what seemed to be an excessive
> amount of time for (many) weeks. I didn't have time to look at why, so I
> would kill it and return to other work. Recently, I just let it run and
> top showed logrotate was still running after 467 minutes (using over 99%
> CPU). After some looking around, I found that I had 100MB in empty error
> messages in /var/log/mailman.
>
> total 0 [actually, over 100MB]
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54 error.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 23 21:54
> error.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1
> -rw-rw-r--    1 mailman  mailman         0 Apr 19 04:45
> error.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1.1
>
> As I tried to delete them I got the following errors:
>
> [root@localhost mailman]# rm -f error.*
> bash: /bin/rm: Argument list too long
>
> [root@localhost mailman]# rm -f error.1*
> bash: /bin/rm: Argument list too long
>
> [root@localhost mailman]# rm -f error.1.1*
> bash: /bin/rm: Argument list too long
>
> [root@localhost mailman]# rm -f error.1.1.1.1*
> bash: /bin/rm: Argument list too long
>
> [root@localhost mailman]# rm -f error.1.1.1.1.1*
> bash: /bin/rm: Argument list too long
>
> [root@localhost mailman]# rm -f error.1.1.1.1.1.1*
> bash: /bin/rm: Argument list too long
>
> I finally found some that I could delete.
>
> [root@localhost mailman]# rm -f error.4.4.3*
>
> [root@localhost mailman]# rm -f error.4.4.2*
>
> [root@localhost mailman]# rm -f error.4.4*
>
> After an hour of this I decided there must be a way to write a bash script
> to go through adding a digit each time rm failed.
>
> * * * * * * Bash script attempt
>
> I have no prior experience with bash scripting, so I am hoping there are
> simple syntax errors someone can point out. The script is "bigerase" and
> the first arg is "error".
>
> [root@localhost mailman]# bigerase error
>
> The idea is that if it can't erase all files "error.1*" because there are
> too many, it calls itself again and tries "error.1.1*", and so forth.
>
> declare -i $i;
> echo $i $1
> for (($i=1; $i<5; $i++));
> Do
>   until rm -f $1.$i* || exec -a bigerase $1.$i;
>   done;
>   echo -n done;
>
> I read through the bash man page 3 times, but couldn't get enough out of
> it to understand the error message: unexpected Do on line 4. The man page
> said the syntax of the for loop was "for ((exp; exp; exp)); do exp; done",
> which is what I thought I had.
>
> Any help would be appreciated. Thanks.

Consider another direction:
$ find /var/log -iname \*.[0-9].[0-9] -print0 | xargs -r0 rm -f

I would expect it to find any files which end with digit period digit and
then delete them.

find's "-print0" allows null terminated names to better encapsulte
whitespaces of file name (in case there are any found) and then xargs with
the -0 deas with the expectation for incoming data to use nulls to
separate found items.

HTH