[NTLUG:Discuss] bash shell programming follies

Steve Baker sjbaker1 at airmail.net
Thu Jun 27 04:46:03 CDT 2002


Richard Geoffrion wrote:
> OK, will someone explain what it was I did and why it affected my system the
> way it did?

> cat /proc/mdstat #show the raid device status
> sleep 3            #pause three seconds
> $0                #rerun this command

> Does a shell script not end when it reaches the last line?

But it never reaches the end of the last line.

   $0 means the zeroth' command line argument - which is the
name of the command.  Hence if your script was called 'fubar',
the shell would replace $0 with 'fubar' - so the script runs
itself - which runs itself - which runs itself...since none
of these scripts-run-by-other-scripts ever finishes, you have
a recursive shell script - each of which runs in it's own copy
of 'bash'.

I'm not 100% sure why you couldn't break out of it - but it's
a nasty fork-bomb that'll rapidly eat up every byte of memory
in your machine if you run it with enough privilage.

It's actually *not* aptly named - it doesn't "loop" - it "recurses",
and cursing is what it'll make you do!

You need something more like:

   while true ; do
     cat /proc/mdstat #show the raid device status
     sleep 3            #pause three seconds
   done

----------------------------- Steve Baker -------------------------------
Mail : <sjbaker1 at airmail.net>   WorkMail: <sjbaker at link.com>
URLs : http://www.sjbaker.org
        http://plib.sf.net http://tuxaqfh.sf.net http://tuxkart.sf.net
        http://prettypoly.sf.net http://freeglut.sf.net
        http://toobular.sf.net   http://lodestone.sf.net






More information about the Discuss mailing list