0

I have a command script i'm making and wondering how do i go about combining multiple options

the general usage of the command is as follows:

Usage: myCommand [-fpr] [-n count] [-s size] [-u user] [-d directory]

My question refers to the [-fpr] option. If the user types myCommand -fpr or myCommand -fp how do I combine/concatenate the options together if it's in a while loop?

while getopts "fpr" opt; do
    case $opt in
    f)
        echo `-l`
        echo "-f was triggered!" >&2
        ;;
    p)
            echo `-h`
        echo "-p was triggered!" >&2
        ;;
    r)
        echo "-r was triggered!" >&2
        ;;

    \?)
        echo "Invalid option: -$OPTARG\n" >&2
        usage
        ;;
    esac
done
1
  • I don't understand your actual question. But echo `-h` does not make sense as it tries to execute -h and then echo it's output...
    – Jasper
    Apr 12, 2014 at 11:13

1 Answer 1

2

If I understand your question correctly, you are trying to figure out how to handle -fpr the same as -f -p -r. In that case, getopts does this for you.

If you want to use the "long option" -fpr and not threat it identical to -f -p -r, then you need to abandon getopts, and look to for example getopt (note the lack of the s at the end), which supports long options. See e.g. http://linux.die.net/man/1/getopt for details.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .