В bash см. help getopts
: «Когда для опции требуется аргумент, getopts помещает этот аргумент в переменную оболочки OPTARG .»
usage() { echo "Usage: $(basename $0) -n name -p port -r"; exit; }
while getopts :n:p:r opt # don't forget the colons for opts that take an arg
do
case $opt in
n ) name="$OPTARG" ;;
p ) port="$OPTARG" ;;
r ) robot=chicken ;;
? ) usage ;;
esac
done
shift $(( OPTIND - 1 ))
echo "the name is $name"
echo "the port is $port"
I 'Я уверен, что вы можете поискать решение для разбора опций в bash.Вот пара минут работы:
#!/bin/bash
usage() { echo foo; exit; }
while [[ $1 == -* ]]; do
case "$1" in
--) shift 1; break ;;
-p|--p|--port) port="$2"; shift 2;;
-n|--n|--name) name="$2"; shift 2;;
*) echo "unknown option: $1"; usage;;
esac
done
echo "the name is $name"
echo "the port is $port"
echo "the rest of the args are:"; ( IFS=,; echo "$*" )
И тест
$ bash longopts.sh --port 1234 --bar a b c
unknown option: --bar
foo
$ bash longopts.sh --port 1234 a b c
the name is
the port is 1234
the rest of the args are:
a,b,c