How to iterate over arguments in a Bash script

后端 未结 8 2585
长发绾君心
长发绾君心 2020-11-22 02:27

I have a complex command that I\'d like to make a shell/bash script of. I can write it in terms of $1 easily:

foo $1 args -o $1.ext
         


        
8条回答
  •  一生所求
    2020-11-22 03:10

    getopt Use command in your scripts to format any command line options or parameters.

    #!/bin/bash
    # Extract command line options & values with getopt
    #
    set -- $(getopt -q ab:cd "$@")
    #
    echo
    while [ -n "$1" ]
    do
    case "$1" in
    -a) echo "Found the -a option" ;;
    -b) param="$2"
    echo "Found the -b option, with parameter value $param"
    shift ;;
    -c) echo "Found the -c option" ;;
    --) shift
    break ;;
    *) echo "$1 is not an option";;
    esac
    shift
    

提交回复
热议问题