l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
October 20: Web Application Hacking: How to Make and Break Security on the Web
Next Installfest:
TBD
Latest News:
Oct. 10: LUGOD Installfests coming again soon
Page last updated:
2004 Apr 28 11:15

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
Re: [vox-tech] Bash scripting newbie - need syntax help
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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



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

_______________________________________________
vox-tech mailing list
vox-tech@lists.lugod.org
http://lists.lugod.org/mailman/listinfo/vox-tech



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
EDGE Tech Corp.
For donating some give-aways for our meetings.