Bash Shell脚本——检查标志并获取其值

时间:2021-09-14 12:06:36

I am trying to make a shell script which is designed to be run like this:

我正在尝试制作一个shell脚本,设计成这样运行:

script.sh -t application

Firstly, in my script I want to check to see if the script has been run with the -t flag. For example if it has been run without the flag like this I want it to error:

首先,在我的脚本中,我想检查脚本是否已使用-t标志运行。例如,如果它运行时没有这样的标志,我希望它出错:

script.sh

Secondly, assuming there is a -t flag, I want to grab the value and store it in a variable that I can use in my script for example like this:

其次,假设有一个-t标记,我希望获取该值并将其存储在一个变量中,我可以在脚本中使用这个变量,例如:

FLAG="application"

So far the only progress I've been able to make on any of this is that $@ grabs all the command line arguments but I don't know how this relates to flags, or if this is even possible.

到目前为止,我所能做的唯一的进展是$@获取所有的命令行参数,但是我不知道这与标记有什么关系,或者这是否可能。

3 个解决方案

#1


86  

You should read this getopts tutorial.

您应该阅读本getopts教程。

Example with -a switch that requires an argument :

一个需要论证的开关的例子:

#!/bin/bash

while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

Like greybot said(getopt != getopts) :

就像葛雷伯特说的(getopt != getopts):

The external command getopt(1) is never safe to use, unless you know it is GNU getopt, you call it in a GNU-specific way, and you ensure that GETOPT_COMPATIBLE is not in the environment. Use getopts (shell builtin) instead, or simply loop over the positional parameters.

外部命令getopt(1)使用起来从来都不安全,除非您知道它是GNU getopt,您可以以特定于GNU的方式调用它,并确保GETOPT_COMPATIBLE不在环境中。使用getopts (shell内置),或者简单地循环位置参数。

#2


25  

Use $# to grab the number of arguments, if it is unequal to 2 there are not enough arguments provided:

使用$#获取参数的数量,如果不等于2,则没有提供足够的参数:

if [ $# -ne 2 ]; then
   usage;
fi

Next, check if $1 equals -t, otherwise an unknown flag was used:

接下来,检查$1是否等于-t,否则使用未知标志:

if [ "$1" != "-t" ]; then
  usage;
fi

Finally store $2 in FLAG:

最后存储$2标记:

FLAG=$2

Note: usage() is some function showing the syntax. For example:

注意:usage()是显示语法的函数。例如:

function usage {
   cat << EOF
Usage: script.sh -t <application>

Performs some activity
EOF
   exit 1
}

#3


0  

Try shFlags -- Advanced command-line flag library for Unix shell scripts.

尝试shFlags——Unix shell脚本的高级命令行标记库。

http://code.google.com/p/shflags/

http://code.google.com/p/shflags/

It is very good and very flexible.

它非常好而且非常灵活。

FLAG TYPES: This is a list of the DEFINE_*'s that you can do. All flags take a name, default value, help-string, and optional 'short' name (one-letter name). Some flags have other arguments, which are described with the flag.

标志类型:这是一个你可以做的定义的列表。所有标志都有一个名称、默认值、帮助字符串和可选的“短”名称(一个字母的名称)。有些标志有其他参数,这些参数用标志来描述。

DEFINE_string: takes any input, and intreprets it as a string.

DEFINE_string:接受任何输入,并将其作为字符串表示。

DEFINE_boolean: typically does not take any argument: say --myflag to set FLAGS_myflag to true, or --nomyflag to set FLAGS_myflag to false. Alternately, you can say --myflag=true or --myflag=t or --myflag=0 or --myflag=false or --myflag=f or --myflag=1 Passing an option has the same affect as passing the option once.

DEFINE_boolean:通常不会使用任何参数:say——myflag将FLAGS_myflag设置为true,或者——nomyflag将FLAGS_myflag设置为false。或者,你可以说——myflag=true或者——myflag=t或者——myflag=0或者——myflag=false或者——myflag=f或者——myflag=1传递一个选项和传递一个选项有相同的效果。

DEFINE_float: takes an input and intreprets it as a floating point number. As shell does not support floats per-se, the input is merely validated as being a valid floating point value.

DEFINE_float:接受输入并将其作为浮点数。由于shell本身不支持浮点数,因此输入仅仅被验证为一个有效的浮点值。

DEFINE_integer: takes an input and intreprets it as an integer.

DEFINE_integer:接受一个输入并将它作为一个整数。

SPECIAL FLAGS: There are a few flags that have special meaning: --help (or -?) prints a list of all the flags in a human-readable fashion --flagfile=foo read flags from foo. (not implemented yet) -- as in getopt(), terminates flag-processing

特殊标志:有一些具有特殊含义的标志:-help(或-?)以人类可读的方式打印所有标志的列表——flagfile=foo read FLAGS。(尚未实现)——如在getopt()中,终止标记处理。

EXAMPLE USAGE:

使用示例:

-- begin hello.sh --
 ! /bin/sh
. ./shflags
DEFINE_string name 'world' "somebody's name" n
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
echo "Hello, ${FLAGS_name}."
-- end hello.sh --

$ ./hello.sh -n Kate
Hello, Kate.

Note: I took this text from shflags documentation

注意:本文取自shflags文档

#1


86  

You should read this getopts tutorial.

您应该阅读本getopts教程。

Example with -a switch that requires an argument :

一个需要论证的开关的例子:

#!/bin/bash

while getopts ":a:" opt; do
  case $opt in
    a)
      echo "-a was triggered, Parameter: $OPTARG" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      exit 1
      ;;
    :)
      echo "Option -$OPTARG requires an argument." >&2
      exit 1
      ;;
  esac
done

Like greybot said(getopt != getopts) :

就像葛雷伯特说的(getopt != getopts):

The external command getopt(1) is never safe to use, unless you know it is GNU getopt, you call it in a GNU-specific way, and you ensure that GETOPT_COMPATIBLE is not in the environment. Use getopts (shell builtin) instead, or simply loop over the positional parameters.

外部命令getopt(1)使用起来从来都不安全,除非您知道它是GNU getopt,您可以以特定于GNU的方式调用它,并确保GETOPT_COMPATIBLE不在环境中。使用getopts (shell内置),或者简单地循环位置参数。

#2


25  

Use $# to grab the number of arguments, if it is unequal to 2 there are not enough arguments provided:

使用$#获取参数的数量,如果不等于2,则没有提供足够的参数:

if [ $# -ne 2 ]; then
   usage;
fi

Next, check if $1 equals -t, otherwise an unknown flag was used:

接下来,检查$1是否等于-t,否则使用未知标志:

if [ "$1" != "-t" ]; then
  usage;
fi

Finally store $2 in FLAG:

最后存储$2标记:

FLAG=$2

Note: usage() is some function showing the syntax. For example:

注意:usage()是显示语法的函数。例如:

function usage {
   cat << EOF
Usage: script.sh -t <application>

Performs some activity
EOF
   exit 1
}

#3


0  

Try shFlags -- Advanced command-line flag library for Unix shell scripts.

尝试shFlags——Unix shell脚本的高级命令行标记库。

http://code.google.com/p/shflags/

http://code.google.com/p/shflags/

It is very good and very flexible.

它非常好而且非常灵活。

FLAG TYPES: This is a list of the DEFINE_*'s that you can do. All flags take a name, default value, help-string, and optional 'short' name (one-letter name). Some flags have other arguments, which are described with the flag.

标志类型:这是一个你可以做的定义的列表。所有标志都有一个名称、默认值、帮助字符串和可选的“短”名称(一个字母的名称)。有些标志有其他参数,这些参数用标志来描述。

DEFINE_string: takes any input, and intreprets it as a string.

DEFINE_string:接受任何输入,并将其作为字符串表示。

DEFINE_boolean: typically does not take any argument: say --myflag to set FLAGS_myflag to true, or --nomyflag to set FLAGS_myflag to false. Alternately, you can say --myflag=true or --myflag=t or --myflag=0 or --myflag=false or --myflag=f or --myflag=1 Passing an option has the same affect as passing the option once.

DEFINE_boolean:通常不会使用任何参数:say——myflag将FLAGS_myflag设置为true,或者——nomyflag将FLAGS_myflag设置为false。或者,你可以说——myflag=true或者——myflag=t或者——myflag=0或者——myflag=false或者——myflag=f或者——myflag=1传递一个选项和传递一个选项有相同的效果。

DEFINE_float: takes an input and intreprets it as a floating point number. As shell does not support floats per-se, the input is merely validated as being a valid floating point value.

DEFINE_float:接受输入并将其作为浮点数。由于shell本身不支持浮点数,因此输入仅仅被验证为一个有效的浮点值。

DEFINE_integer: takes an input and intreprets it as an integer.

DEFINE_integer:接受一个输入并将它作为一个整数。

SPECIAL FLAGS: There are a few flags that have special meaning: --help (or -?) prints a list of all the flags in a human-readable fashion --flagfile=foo read flags from foo. (not implemented yet) -- as in getopt(), terminates flag-processing

特殊标志:有一些具有特殊含义的标志:-help(或-?)以人类可读的方式打印所有标志的列表——flagfile=foo read FLAGS。(尚未实现)——如在getopt()中,终止标记处理。

EXAMPLE USAGE:

使用示例:

-- begin hello.sh --
 ! /bin/sh
. ./shflags
DEFINE_string name 'world' "somebody's name" n
FLAGS "$@" || exit $?
eval set -- "${FLAGS_ARGV}"
echo "Hello, ${FLAGS_name}."
-- end hello.sh --

$ ./hello.sh -n Kate
Hello, Kate.

Note: I took this text from shflags documentation

注意:本文取自shflags文档