如何防止将参数传递给在 getopt 中不带参数的参数?
How to prevent passing parameters to arguments that don't take one in getopt?
例如考虑这个脚本,
#!/bin/sh
OPTS=`getopt -o ahb:c: --long help,name:,email: -- "$@"`
#echo "$OPTS"
eval set -- "$OPTS"
usage () {
echo "type -h for help"
}
while true; do
case "" in
-a) echo "a is a short option that do not take parameters"; shift ;;
-b) echo "b is a short option that requires one parameter and you specified "; shift 2;;
-c) echo "c is a short option that requires one parameter and you specified "; shift 2;;
--name) echo "your name is "; shift 2;;
--email) echo "your email is "; shift 2;;
-h | --help) echo "Google search yourself !"; shift 1;;
--) usage ; shift; break ;;
*) echo "hello"; break ;;
esac
done
因此,如果将脚本调用为 sh myscript.sh -a hello
,它应该抛出一个错误,告知 -a 不接受任何参数。
有办法吗?
您遇到的问题是因为您不需要 "second" 参数 但 它实际上是第三个参数!
如果您不需要-a 后的任何参数,那么您需要检查$3 是否存在。 ($2 将是 '--')
这是固定代码,在 -a 之后提供内容时会打印错误:
#!/bin/sh
OPTS=`getopt -o ahb:c: --long help,name:,email: -- "$@"`
#echo "$OPTS"
eval set -- "$OPTS"
usage () {
echo "type -h for help"
}
while true; do
case "" in
-a) if [ ! -z "" ] ; then
#Check if there is something after -a
echo "a is a short option that do not take parameters";
else
echo "Whatever..";
fi
shift ;;
-b) echo "b is a short option that requires one parameter and you specified "; shift 2;;
-c) echo "c is a short option that requires one parameter and you specified "; shift 2;;
--name) echo "your name is "; shift 2;;
--email) echo "your email is "; shift 2;;
-h | --help) echo "Google search yourself !"; shift 2;;
--) usage ; shift; break ;;
*) echo "hello"; break ;;
esac
done
希望这就是您要找的:)
例如考虑这个脚本,
#!/bin/sh
OPTS=`getopt -o ahb:c: --long help,name:,email: -- "$@"`
#echo "$OPTS"
eval set -- "$OPTS"
usage () {
echo "type -h for help"
}
while true; do
case "" in
-a) echo "a is a short option that do not take parameters"; shift ;;
-b) echo "b is a short option that requires one parameter and you specified "; shift 2;;
-c) echo "c is a short option that requires one parameter and you specified "; shift 2;;
--name) echo "your name is "; shift 2;;
--email) echo "your email is "; shift 2;;
-h | --help) echo "Google search yourself !"; shift 1;;
--) usage ; shift; break ;;
*) echo "hello"; break ;;
esac
done
因此,如果将脚本调用为 sh myscript.sh -a hello
,它应该抛出一个错误,告知 -a 不接受任何参数。
有办法吗?
您遇到的问题是因为您不需要 "second" 参数 但 它实际上是第三个参数!
如果您不需要-a 后的任何参数,那么您需要检查$3 是否存在。 ($2 将是 '--')
这是固定代码,在 -a 之后提供内容时会打印错误:
#!/bin/sh
OPTS=`getopt -o ahb:c: --long help,name:,email: -- "$@"`
#echo "$OPTS"
eval set -- "$OPTS"
usage () {
echo "type -h for help"
}
while true; do
case "" in
-a) if [ ! -z "" ] ; then
#Check if there is something after -a
echo "a is a short option that do not take parameters";
else
echo "Whatever..";
fi
shift ;;
-b) echo "b is a short option that requires one parameter and you specified "; shift 2;;
-c) echo "c is a short option that requires one parameter and you specified "; shift 2;;
--name) echo "your name is "; shift 2;;
--email) echo "your email is "; shift 2;;
-h | --help) echo "Google search yourself !"; shift 2;;
--) usage ; shift; break ;;
*) echo "hello"; break ;;
esac
done
希望这就是您要找的:)