Linux 在没有 getopts 的 shell 脚本中添加用法内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14975057/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:04:42  来源:igfitidea点击:

Add usage content in shell script without getopts

linuxshellunixgetopts

提问by San

I have script where i need to display usage command in case user miss any mandatory information while executing script.

我有脚本,我需要在其中显示使用命令,以防用户在执行脚本时错过任何必需信息。

Usage : Script -s <server> -i <instance> -u <user> -p <password> <query> -w <warning value> -c <critical value>

With explanation about all the OPTIONS

关于所有的解释 OPTIONS

I'm getting values from arguments as below variables fashion. But I want this usage with validations in shell script.

我从参数中获取值,如下变量时尚。但我希望这种用法与 shell 脚本中的验证一起使用。

SERVER=
INSTANCE=
USER=
DB_PASSWD=
QUERY=
VAL_WARN=
VAL_CRIT=

I have tried using getopts, But failed to use since <query>doesn't have a -qparameter before passing the value.

我曾尝试使用 getopts,但未能使用,因为在传递值之前<query>没有-q参数。

I have tried finding all other ways, But everyone suggested getopts which is not feasible solution for me.

我尝试过寻找所有其他方法,但每个人都建议使用 getopts,这对我来说不是可行的解决方案。

Please help me on this..

请帮我解决这个..

采纳答案by matt.nguyen

Use shift to iterate through all of your arguments, something like:

使用 shift 遍历所有参数,例如:

#!/bin/sh

usage ()
{
  echo 'Usage : Script -s <server> -i <instance> -u <user> -p <password>'
  echo '                  <query> -w <warning value> -c <critical value>'
  exit
}

if [ "$#" -ne 13 ]
then
  usage
fi

while [ "" != "" ]; do
case  in
        -s )           shift
                       SERVER=
                       ;;
        -i )           shift
                       INSTANCE=
                       ;;
        -u )           shift
                       USER=
                       ;;
        -p )           shift
                       PASSWORD=
                       ;;
        -w )           shift
                       WARNINGVAL=
                       ;;
        -c )           shift
                       CRITICVAL=
                       ;;
        * )            QUERY=
    esac
    shift
done

# extra validation suggested by @technosaurus
if [ "$SERVER" = "" ]
then
    usage
fi
if [ "$INSTANCE" = "" ]
then
    usage
fi
if [ "$USER" = "" ]
then
    usage
fi
if [ "$PASSWORD" = "" ]
then
    usage
fi
if [ "$QUERY" = "" ]
then
    usage
fi
if [ "$WARNINGVAL" = "" ]
then
    usage
fi
if [ "$CRITICVAL" = "" ]
then
    usage
fi

echo "ALL IS WELL. SERVER=$SERVER,INSTANCE=$INSTANCE,USER=$USER,PASSWORD=$PASSWORD,QUERY=$QUERY,WARNING=$WARNINGVAL,CRITIC=$CRITICVAL"

Should do the trick.

应该做的伎俩。

EDIT:added argument validation in the script as suggested by @technosaurus

编辑:按照@technosaurus 的建议在脚本中添加参数验证

回答by just somebody

getoptsis bitching for a good reason. you should change your script's interface to conform to what people expect.

getopts是有充分理由的。您应该更改脚本的界面以符合人们的期望。

alternatively, you could use getoptstwice, first for the pre-queryoptions, shift, then for the rest.

或者,您可以使用getopts两次,首先用于预query选项shift,然后用于其余选项。

回答by Balram

try this out

试试这个

usage()
{
   echo "
SERVER=blah INSTANCE=foo Script 
-s <server> -i <instance> -u <user> -p <password> <query> -w <warning value> -c <critical value>" } for i in {0..12} do arg=`expr $i +1` test ! "${!arg}" && usage && break done

hope this helps

希望这可以帮助

回答by William Pursell

This is a non-standard approach, but one that I find very useful. Instead of passing values as arguments to specific flags (which is fairly annoying; the user should not be required to specify every value but reasonable defaults should be supplied), you can simply pass them directly via the environment, so that a typical call would look like:

这是一种非标准的方法,但我发现它非常有用。而不是将值作为参数传递给特定标志(这很烦人;不应该要求用户指定每个值,但应提供合理的默认值),您可以简单地直接通过环境传递它们,以便典型的调用看起来喜欢:

##代码##

It would be nice if you used lower case variable name so the user doesn't have to shout. This allows the script so simply avoid parsing the command line completely, as the values of the variables will be set when the script begins.

如果您使用小写的变量名,那么用户就不必大喊大叫了。这允许脚本如此简单地避免完全解析命令行,因为将在脚本开始时设置变量的值。