[英]How to echo when there is no input in getopts command
This is put before the case statements. 这放在case语句之前。 It still outputs nothing when executed without any input. 在没有任何输入的情况下执行时,它仍然不输出任何内容。
while getopts :x:y:z: OPT; do
if [ $OPT == "" ]; then
echo "Null"
exit 10
fi
also, how should I code this to execute the values in any position/order? 另外,我应该如何编码以执行任何位置/顺序的值? such as: 如:
.\project -x 120 -y 170 -z Car
.\project -y 170 -z Car -x 120
You can first test if no arguments were passed at all and exit (this happens before the getopts loop): 您可以首先测试是否没有传递任何参数并退出(这发生在getopts循环之前):
if [ $# -eq 0 ]; then
usage # Call usage function or echo some usage message
exit
fi
There's no way of forcing mandatory arguments. 无法强制使用强制性参数。 What you can do is test for a certain value after the getopts loop has finished. 您可以做的是在getopts循环完成后测试某个值。
If -x
is a mandatory option, and it is used to set the var
variable, test to see if it hasn't been set by the getopts loop, and if so print a relevant message and exit: 如果-x
是强制性选项,并且用于设置var
变量,请测试以查看getopts循环是否尚未设置该变量,如果是,则打印相关消息并退出:
if [ -z ${var+x} ]; then
echo "No -x argument supplied. exiting"
exit 10
fi
The above runs after the case statement. 以上在case语句之后运行。
Also, you're missing a 'done' after your while loop, most likely a typo. 另外,您在while循环之后会错过“完成”操作,很可能是拼写错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.