
Após 2 argumentos, o início de cada argumento não é igual a -*
for args in "$@"
do
if [[ ${@: 2} != -* ]]; then
case "$args" in
-q)
if [ ! -z "$2" ]; then
echo "$2"
shift
fi
shift
;;
-w)
if [ ! -z "$2" ]; then
echo "$2"
shift
fi
shift
;;
-e)
if [ ! -z "$2" ]; then
echo "$2"
shift
fi
shift
;;
esac
else
echo "arguments start with '-'"
fi
done
Só funciona com o primeiro argumento
-q s d f g h
corretamente
-q -v -b -n -m -n
e -q -l j u -y d
errado
Após o primeiro argumento, os demais argumentos não devem começar com o caractere '-'
if [ ! -z "$2" ];
- verifica se o argumento está vazio
Responder1
Parece que você deseja verificar se nenhum dos argumentos, exceto o primeiro, começa com um travessão.
Você pode fazer isso assim:
#!/bin/bash
if [[ $1 != -* ]]; then
printf '1st argument, "%s", does not start with a dash\n' "$1"
exit 1
fi >&2
arg1=$1
shift
for arg do
if [[ $arg == -* ]]; then
printf 'Argument "%s" starts with a dash\n' "$arg"
exit 1
fi
done >&2
echo 'All arguments ok'
printf 'arg 1 = "%s"\n' "$arg1"
printf 'other arg = "%s"\n' "$@"
Se você precisar que seu primeiro argumento seja -q
específico, altere o primeiro teste de
[[ $1 != -* ]]
para
[[ $1 != -q ]]