Writing Unix script to accept arguments

Hi,

This may be answered elsewhere but I wasn't entirely sure of the wording I should use to search so here we go with an attempt:

I wish to make a script that will allow commands to be passed to it such as:

<command> -oOPTIONS -aANOTHER -pRINT

etc

However I don't really know the syntax of what to do ir how to accept them with in the code.

Like for example:

If I want it do run a certain command if -o equals "Fred".

If anyone has any help or "how to's" it would be most apprieciated.

Take a look at getopts (about 3/4 down the page)

Another simple example in bash (if you do not care about the switches' names) :

#!/bin/bash

parseArguments() {
    if [[ $# -lt 3 ]]; then
        echo "Usage: $0 host user pass"
        exit 1
    fi
host="$1"
user="$2"
pass="$3"

# do you stuff here, and put the following at the main part of the script : 
parseArguments() $@

The $0 is the name of the script.

In pure bourne shell...

f=0
while [ $# -gt 0 ]; do
        case "$1" in
        -o?*)
                # handles things like -oValue
                o=`expr "$1" : '..\(.*\)'`
        ;;
        -o)
                # handles things like -o Value
                o="$2"
                shift
        ;;
        -f)
                # Just a flag (on/off)
                f=1
        ;;
        *)
                break
        ;;
        esac
        shift
done
echo "o=$o, f=$f"
# Loop through remaining arguments (arguments without a hyphen)
for arg in "$@"; do
        echo "$arg"
done