several programs that do the same kinds of things,
programs that use a lot of the same code (as you're writing the second, third, etc., programs, you copy a lot of lines from the first program), or
a program with several options that make a big change in the way it works,
you might want to write just one program and maketo it instead. The program can find the name you called it with and, through case or test commands, work in different ways. For instance, the Berkeley UNIX commands ex, vi, view, edit, and others are all links to the same executable file. This takes less disk space and makes maintenance easier. It's usually only sensible when most of the code is the same in each program. If the program is full of name tests and lots of separate code, this technique may be more trouble than it's worth.
Depending on how the script program is called, this name can be
relative pathname like
./prog-it can also be an
absolute pathname like
There are a couple of ways to handle this in a shell script.
If there's just one main piece of code in the script, as in the
a case that tests
$0 might be best.
The asterisk (
*) wildcard at the start of each case
different pathnames that might be used to call the script:
case "$0" in *
name1) ...do this when called as name1... ;; *
name2) ...do this when called as name2... ;; ... *) ...print error and exit if $0 doesn't match... ;; esac
myname=`basename $0` ... case "$myname" in ... echo "$myname: aborting; error in xxxxxx" 1>&2 ...