
2개의 인수 이후 각 인수의 시작이 -*와 같지 않습니다.
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
첫 번째 인수에서만
-q s d f g h
올바르게 작동합니다.
-q -v -b -n -m -n
그리고 -q -l j u -y d
틀렸어
첫 번째 인수 이후 나머지 인수는 '-' 문자로 시작하면 안 됩니다.
if [ ! -z "$2" ];
- 인수가 비어 있는지 확인
답변1
첫 번째 인수를 제외하고 대시로 시작하는 인수가 없는지 확인하려는 것 같습니다.
다음과 같이 할 수 있습니다:
#!/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' "$@"
첫 번째 인수가 구체적으로 필요한 경우 -q
첫 번째 테스트를 다음에서 변경하십시오.
[[ $1 != -* ]]
에게
[[ $1 != -q ]]