Everything may fail one day (was Re: Interactive 2.2 File zapper.)

Martin Weitzel martin at mwtech.UUCP
Thu Jul 26 05:21:21 AEST 1990


In article <772 at essnj1.ESSNJAY.COM> smitty at essnj1.ESSNJAY.COM (Hibbard T. Smith JR) writes:
[some lines deleted]
[
>--	cd /lost+found
>--	find . -mtime +14 -exec rm -rf {} \;
>If there's no lost and found directory in the root file system, this deletes
>everything in the system that's older than 14 days. Two possible fixes exist:
>-- cd /lost+found && find . -mtime +14 -exec rm -rf {} \;
>-- find /lost+found -mtime +14 -exec -rm {} \;

Nice example. (I'll save this one as a `war story' for my courses.)
I'm curious what more of this sort will be found .. :-) .. :-|... :-(

In production code I generally advise to stick to a very simple rule:

********************************************************************
**** Every operation, repeat EVERY operation, may fail one day. ****
********************************************************************

It's a pitty that the good ol' "&&" and "||" operators of the shell
tends to become forgotten since the shell has if-then now. Unfortunately
the latter tend to clutter up things so far that some may feel reluctant
to write:

	if cd /lost+found
	then	find . -mtime +14 -exec rm -rf {} \;
	else	echo "$0: Panic! No /lost+found" >&2
		exit 99
	fi

Please people, before you execute operations (here: find . ... rm -rf)
that depend heavily on a certain state (here: working directory is
/lost+found) make sure the operations which lead to that state didn't
fail. For shell scripts I found the following style well readable

	cd /lost+found || { echo "$0: Panic! No /lost+found" >&2; exit 99; }

especially when the shell supports functions and you can abbreviate
this to:

	# central at the start
	panic() {
		echo "$0: Panic! $1" >&2
		exit ${2-99}
	}
	......
	# later wherever you need it
	cd /lost+found || panic "No /lost+found"
-- 
Martin Weitzel, email: martin at mwtech.UUCP, voice: 49-(0)6151-6 56 83



More information about the Comp.unix.i386 mailing list