Bash Shell Script - Check for a flag and grab its value

前端 未结 4 1885
遥遥无期
遥遥无期 2021-01-29 22:21

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

script.sh -t application

Firstly, in my script I want to check to see

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-29 22:45

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

    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_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_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_integer: takes an input and intreprets it as an 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

    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

提交回复
热议问题