Case Statement

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:
    Hey, guys I really need some help with a project.

"Write a shell program that examines the command line arguments, counts and collects the number of options. Basically it has to collect and count the arguments that start with a "-" and the one's that don't start with a -

I know I have to use -*) and *) I am having trouble actually creating the structure

  1. Relevant commands, code, scripts, algorithms:

  2. The attempts at a solution (include all code and scripts):

I tried using some commands but I am not sure how case statement works

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

SAC, San Antonio, TX Baker Unix

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

Hi, have a look at getopts

Please mention what Shell you are using.

from bash's "help case": case WORD in [PATTERN [| PATTERN]...) COMMANDS ;;]... esac

So your starting point:

    case "$1" in
        -*) command;;
        *) command;;
    esac

You will want to look at shift and while so that you can loop across all command line arguments passed to the shell.

Yes, I am using bash.

I've try using commands and it tells me it dosen't exist or i can't find that command what might I be doing wrong or do I have to structure that command in a specific way.

show us. copy and paste what you try and what it does

case "$1" in
-*) echo "Total of is $#";;
echo " Collected $@";;
*) echo "Total of non- is $#";;
echo "Collected $@";;
esac

I am not sure if this is even right but I know $# displays the total number of arguments and $@ shows the actual arguments but my results are that is showing all of them even for -*) still shows the ones without the -.

It's only testing the first argument -- why should that stop it from showing all non dashed arguments?

It's an example, showing you the pieces you need to build it without doing all the work for you.

the ;; is not to separate commands, but sections of case. like a break; in C if you are familiar...

this does not count at all. you need a loop.

while [[ $1 ]]; do
    case "$1" in
    -*) increment a variable
        something else maybe
            ;;    # this ;; mean end of "-*" match
    *)  increment other variable
        ;;
    esac

    shift    # this moves $1 to be next option on command line
done