Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash script parameters [duplicate]

I need to write a bash script, and would like it to parse unordered parameters of the format:

scriptname --param1 <string> --param2 <string> --param3 <date>

Is there a simple way to accomplish this, or am I pretty much stuck with $1, $2, $3?

like image 749
Brent Avatar asked May 06 '09 02:05

Brent


2 Answers

You want getopts.

like image 121
Lance Richardson Avatar answered Sep 23 '22 19:09

Lance Richardson


while [[ $1 = -* ]]; do
    arg=$1; shift           # shift the found arg away.

    case $arg in
        --foo)
            do_foo "$1"
            shift           # foo takes an arg, needs an extra shift
            ;;
        --bar)
            do_bar          # bar takes no arg, doesn't need an extra shift
            ;;
    esac
done
like image 22
lhunath Avatar answered Sep 21 '22 19:09

lhunath