Pergunta

I have an ash script where I need to check whether the user has entered anything stupid. The proper use is:

script <read | monitor> -s <system | event> [-f filter] [-n number]

And I need to detect if user has for example evoked it like:

script read -s system dummydummy

In order to parse the proper arguments, I use

while getopts "s:f:n:" Option
do
  case $Option in
    s)logname=${OPTARG};;
    f)filterspec=${OPTARG};;
    n)numlines=${OPTARG};;
    *)exit $E_OPTERROR;;   # DEFAULT
  esac
  OPRIND=${OPTIND}
done

How can I detect if there are any leftover unparsed arguments?

Foi útil?

Solução

You can use this after exiting the while-loop:

shift $(($OPTIND - 1))
echo "Remaining arguments: $@"

It works in bash-like shells, let us know if it works in ash too.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top