How to do it in sh

Commonly used sh techniques, in no particular order

If there's something you're looking for on this page and not finding, or if you have a correction, let us know

Make a scratch directory

Make a directory under /tmp with the program name concatenated to the current process number. E.g., if your script is named "wilco",
	DIRNAME=/tmp/wilco.$$
	mkdir $DIRNAME
 

Does a file/directory exist?

Use test, a.k.a "[":
	if [ -f filename ]
	then
	   echo filename exists
	else
	   echo filename does not exist
	fi

Below 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.

Ask a yes or no question

use "echo" for the prompt, "read" for the answer, and "case" to process it. Minimally:
	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 ;;
        esac

Of 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.)

What is this script's name?

For error messages, you sometimes want to have a script give it's name (in case it's called from another script, for example). You can say:
	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 support@world.std.com

        Copyright 1996 Software Tool & Die, Inc.  All Rights Reserved.

   Software Tool & Die, Inc., 1330 Beacon St. Suite 215 Brookline, MA 02146