Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ getopt(1) — Sprite KS.390

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

csh(1)

sh(1)

getopt(3)

GETOPT  —  User Commands

NAME

getopt − format flags for shell scripts

SYNOPSIS

getopt flag_spec argument ... 

DESCRIPTION

Getopt is a program intended to be called by scripts to “canonicalize” their arguments before processing them, just as the getopt(3) routine does for C programs. (This need for scripts is usually most noticeable in the way lint(1) handles the −n flag.) 

The following two examples provide the initial parsing for a script which takes two flags, −a and −b, the second of which takes an argument. 

# For /bin/csh...
set argv = (‘getopt "ab:" $∗‘)
if ( $status ) then
echo "Read the documentation and try again." >/dev/tty
exit 1
endif
set Aflag=0
set Name=NONE
while "$1" != "--"
switch ("$1")
case ’-a’:
set Aflag=1
breaksw
case ’-b’:
shift
set Name=$1
breaksw
endsw
shift
end
shift
echo Aflag=$Aflag / Name=$Name / Remaining args are $∗
 # For /bin/sh...
set -- ‘getopt "d:s" $@‘
if test $? != 0 ; then
echo "Read the documentation and try again."
exit 1
fi
Aflag=0
Name=NONE
for f
do
case "$f" in
-a)Aflag=1
;;
-b)shift
Name=$2
;;
--)break
;;
esac
shift
done
shift
echo Aflag=$Aflag / Name=$Name / Remaining args are $∗

DIAGNOSTICS

The program burps the standard getopt(3) diagnostics to standard error, and exits with a non-zero status if an error occurs. It is arguable wrong that the diagnostics imply that the program is named “getopt” rather than the real name of the script. It is undeniably AT&T−compatible to do this, however.

SEE ALSO

csh(1), sh(1), getopt(3)

AUTHOR

Rich $alz
Mirror Systems
(mirror!rs, rs@mirror.TMC.COM)

Sprite version 1.0  —  LOCAL

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026