You are here

Case the joint

I have no idea where I found this little script but note the use of the case function on line 7. It is no real eye opener but it is an intelligent and fast method of parsing the command line options of the script.

#!/bin/bash
# -*- shell-script -*-

for i in $*
do
case $i in
--uid|-u) OPT_UID=$2; shift 2;;
--groups|-G) OPT_GROUPS=$2; shift 2;;
--gid|-g) OPT_GROUP=$2; shift 2;;
--home-dir|-d) OPT_HOMEDIR=$2; shift 2;;
--shell|-s) OPT_SHELL=$2;shift 2;;
--non-unique|-o) OPT_NONUNIQUE=1;shift 2;;
--comment|-c) OPT_COMMENT=$2;shift 2;;
esac
done

OPTS=""

if [ -n "$OPT_$HOMEDIR" ]
then
OPTS="$OPTS -d $OPT_HOMEDIR"
fi

if [ -n "$GROUP" ]
then
OPTS="$OPTS -g $OPT_GROUP"
fi

if [ -n "$OPT_GROUPS" ]
then
OPTS="$OPTS -G $OPT_GROUPS"
fi

if [ -n "$OPT_SHELL" ]
then
OPTS="$OPTS -s $OPT_SHELL"
fi

if [ -n "$OPT_UID" ]
then
OPTS="$OPTS -u $OPT_UID"
fi

if [ -n "$OPT_COMMENT" ]
then
OPTS="$OPTS -c \"$OPT_COMMENT\""
fi

if [ -n "$OPT_NOUNIQUE" ]
then
OPTS="$OPTS -o"
fi

CMD=adduser

UNAME=`uname`

case $UNAME in
Solaris) CMD=useradd;break;;
esac

$CMD $OPTS $*

If anyone has seen this script before and knows where it came from, please let me know so i can put the credits here.

Thanks