Parsing shell script arguments

后端 未结 4 1791
渐次进展
渐次进展 2020-12-14 11:35
$myscript.sh -host blah -user blah -pass blah

I want to pass arguments into it.

I\'m used to doing $1, $2,

4条回答
  •  伪装坚强ぢ
    2020-12-14 12:30

    Here is a simple way to handle both long and short options:

    while [[ $1 == -* ]]; do
        case "$1" in
          -h|--help|-\?) show_help; exit 0;;
          -v|--verbose) verbose=1; shift;;
          -f) if (($# > 1)); then
                output_file=$2; shift 2
              else 
                echo "-f requires an argument" 1>&2
                exit 1
              fi ;;
          --) shift; break;;
          -*) echo "invalid option: $1" 1>&2; show_help; exit 1;;
        esac
    done
    

    From How can I handle command-line arguments (options) to my script easily?

提交回复
热议问题