[K12OSN] rsync

Les Mikesell les at futuresource.com
Mon Oct 10 17:07:36 UTC 2005


On Mon, 2005-10-10 at 11:17, Petre Scheie wrote:
> My only hesitation about cd'ing into a directory and then doing something in that 
> directory, instead of just doing the work using a fully qualified path to the directory, 
> stems from a script that a colleague of mine wrote some years ago.  The script would cd 
> into the directory, then do a find for files older than one week, and delete them; it's 
> purpose was to get rid of old log files for a particular app.  However, I didn't know 
> about the script, and one day removed the directory in question.  The next time the 
> script ran, while the cd failed, the find & remove did not fail and it began wiping out 
> files from / on down, crashing the system.  Ever since then, I always specify full paths 
> in my scripts.

The original bourne sh used to exit on a failed cd in a script just to
prevent that sort of thing, but it was changed later and bash has never
done it.  However the 'right' way is to check for errors on a cd, like
cd somewhere || exit 2
..rest of script...
The || is a 'logical or' and executes if the statement to the left
fails (returns non-zero).
Or if you can do your work in one line, reverse it:
cd somewhere && do_something_in_directory
Statements to the right of && 'logical and' only execute if the one
to the left succeeds (returns zero).
Or if you like to be more verbose you can test the value of $? after
any statement to determine success or failure.

-- 
  Les Mikesell
    les at futuresource.com





More information about the K12OSN mailing list