As the thread title implies, getopt is the way to go. Here's a simple example:
Code:
#!/bin/bash
set -e
OPTS=`getopt -n $0 -o p:h: --long port:,host: -- $@`
eval set -- "$OPTS"
while true
do
case "$1" in
-p|--port) echo "port = $2"; shift 2 ;;
-h|--host) echo "host = $2"; shift 2 ;;
--) shift ; break ;;
esac
done
Here, both -p (or --port) and -h (or --host) require one to input values; the ':' that is placed after the options p and h above, imply this. If an arg is not required, then don't use the ':' after the option.