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

前端 未结 4 1876
遥遥无期
遥遥无期 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 23:09

    Here is a generalized simple command argument interface you can paste to the top of all your scripts.

    #!/bin/bash
    
    declare -A flags
    declare -A booleans
    args=()
    
    while [ "$1" ];
    do
        arg=$1
        if [ "${1:0:1}" == "-" ]
        then
          shift
          rev=$(echo "$arg" | rev)
          if [ -z "$1" ] || [ "${1:0:1}" == "-" ] || [ "${rev:0:1}" == ":" ]
          then
            bool=$(echo ${arg:1} | sed s/://g)
            booleans[$bool]=true
            echo \"$bool\" is boolean
          else
            value=$1
            flags[${arg:1}]=$value
            shift
            echo \"$arg\" is flag with value \"$value\"
          fi
        else
          args+=("$arg")
          shift
          echo \"$arg\" is an arg
        fi
    done
    
    
    echo -e "\n"
    echo booleans: ${booleans[@]}
    echo flags: ${flags[@]}
    echo args: ${args[@]}
    
    echo -e "\nBoolean types:\n\tPrecedes Flag(pf): ${booleans[pf]}\n\tFinal Arg(f): ${booleans[f]}\n\tColon Terminated(Ct): ${booleans[Ct]}\n\tNot Mentioned(nm): ${boolean[nm]}"
    echo -e "\nFlag: myFlag => ${flags["myFlag"]}"
    echo -e "\nArgs: one: ${args[0]}, two: ${args[1]}, three: ${args[2]}"
    

    By running the command:

    bashScript.sh firstArg -pf -myFlag "my flag value" secondArg -Ct: thirdArg -f
    

    The output will be this:

    "firstArg" is an arg
    "pf" is boolean
    "-myFlag" is flag with value "my flag value"
    "secondArg" is an arg
    "Ct" is boolean
    "thirdArg" is an arg
    "f" is boolean
    
    
    booleans: true true true
    flags: my flag value
    args: firstArg secondArg thirdArg
    
    Boolean types:
        Precedes Flag(pf): true
        Final Arg(f): true
        Colon Terminated(Ct): true
        Not Mentioned(nm): 
    
    Flag: myFlag => my flag value
    
    Args: one => firstArg, two => secondArg, three => thirdArg
    

    Basically, the arguments are divided up into flags booleans and generic arguments. By doing it this way a user can put the flags and booleans anywhere as long as he/she keeps the generic arguments (if there are any) in the specified order.

    Allowing me and now you to never deal with bash argument parsing again!

    You can view an updated script here

    This has been enormously useful over the last year. It can now simulate scope by prefixing the variables with a scope parameter.

    Just call the script like

    replace() (
      source $FUTIL_REL_DIR/commandParser.sh -scope ${FUNCNAME[0]} "$@"
      echo ${replaceFlags[f]}
      echo ${replaceBooleans[b]}
    )
    

    Doesn't look like I implemented argument scope, not sure why I guess I haven't needed it yet.

提交回复
热议问题