DIRNAME=/tmp/wilco.$$ mkdir $DIRNAME
if [ -f filename ] then echo filename exists else echo filename does not exist fiBelow we check if a directory exists. If it does, exist, we test to see if it is either not writable or not executable. If so, we fail. If it doesn't exist we create it. If it exists but isn't readable, the script quits with an error message.
DIRNAME=/tmp/catchall if [ -d $DIRNAME ] then if [ ! -w $DIRNAME -o ! -x $DIRNAME ] then echo $DIRNAME has to be writable and executable exit fi else mkdir $DIRNAME fi cd $DIRNAME"test" can perform numerous other tests -- whether a file is greater than 0 bytes, whether a file is a symbolic link, etc. See the man page for "test" for documentation.
SOURCE=.login TARGET=.login.bak echo rename $SOURCE to $TARGET? read ANSWER case "$ANSWER" in [yY]*) cp $SOURCE $TARGET ;; [nN]*) echo aborting exit ;; *) echo bad answer ;; esacOf course, you usually want to keep asking 'til the typist provides you with a yes or no answer, so you'll probably want to enclose the construct in a loop. E.g.:
HAVEANSWER=no echo -n "Remove potato? [yn]: " while [ "$HAVEANSWER" != "yes" ] do read ANSWER case "$ANSWER" in [yY]*) echo bye bye potato . . . rm potato HAVEANSWER=yes ;; [nN]*) echo "fine. I'll leave it there then." HAVEANSWER=yes ;; *) echo -n "please answer yes or no... remove potato? [yn]: " ;; esac done(the "echo -n" means echo without appending a newline, it's for aesthetics.)
echo "$0: Error: brain must be engaged prior to use"(The "zeroeth" argument of a program is its own name.) That's adequate, but for aesthetic reasons, you might want just the file name. The external (i.e., not builtin and not necessarily available on every single UNIX machine but available here on World) command to use is basename. "basename /1/2/3" will answer "3". So:
MYNAME=`basename $0` ... echo "$MYNAME: Error: ..."
HOME Top of Help Desk Eye On The World The World Kiosk
For further assistance, please send an email request to email@example.com Copyright 1996 Software Tool & Die, Inc. All Rights Reserved. Software Tool & Die, Inc., 1330 Beacon St. Suite 215 Brookline, MA 02146