Understanding Getopts in an otherwise easy assignment

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:

In the command line I will be given files or words which will be USER ID's or a file of USER ID's. I am to let the user of the script know when any of these individuals log in. I can do it a certain why, which is unacceptable.

I could do this without the getopts command, instead I would just write to files and grep each line (each line would be a user ID) and finger it to see if they are on or not.

My problem is after looking all over for information on getopts, it's not making sense to me. I've been messing around with this exampe:

while getopts ":afz" option;
do
    case $option in
        a)
            echo recieved -a
            ;;
        f)
            echo recieved -f
            ;;
        z)
            echo recieved -z
            ;;
        *)
            echo "invalid option -$OPTARG"
            ;;
        esac
    done
  1. Relevant commands, code, scripts, algorithms:

Mainly getopts is encouraged to be used.

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

With the getopts I've been messing with, I realize if I type anything but -a, -f, or -z I will get the *) option which is "invalid option". if I type -z -a or -f, it will of course echo the received thing. My problem is how can I make -a -z -f options to see if file is readable, or to see if the user is a valid userid, or to see if they are even on. Basically, I am obviously not putting 2 and 2 together to see how getopt is handy in this case. I'm assuming if i make -z -a or -f various options like I stated, it ought to work, but honestly, I don't know how to do this, very frustrating.

What I'm asking perhaps, is if I can see an example that is related to my issue, and perhaps an explanation to allow me to complete this.

If I could see how this getopts thing is used for such a case, I'm sure I could do it, because I can do it without getopts!

I've been looking at alternative solutions, but due to scoping issues, I see no other option but to write to a file (which is not allowed for this assignment). and parse the file line by line using finger/grep.

  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
    USML, St. Louis, Unix Programming, actually this link has it all:
    //*******/~antognolij/cs2750.html
    replace ******* with umsl.edu
    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).

I don't really understand your question. But there are plenty of getopts examples that have been posted to this site. This thread: /t/is-there-a-way-scroll-text-instead-of-page/140647/1 has a script I wrote a while back that uses getopts. Does that help?

Have you written a man page for your shell script?
What does the SYNOPSIS section of your man page look like?
How are the -a , -f , and -z options supposed to affect the behavior of your script?

I could guess that the -f option for your script is supposed to take a filename option-argument specifying a file containing a list of user names (which means that your getopts option list should have a : after the f; or that -f means that the operand(s) to your shell script are filenames of files containing user names rather than having user names as operands.

Have you tried looking at the POSIX man page for getopts available on this site: getopts(1P)? Do you have questions about how the example in that man page works?

Well I was provided with:

alert -m "watch out" xyj67
alert -w "noon oct 29" -m "please send info" xyj67

The xyj67 being the user id.

So I suppose I am to use -m and -w, I suppose my problem is I don't know how to implement getopt to parse this information.

Is it as simple as the user typing in -w someword

I suppose my trouble is how to actually use the words/strings after the case -m or -w.
I'm playing around with this, from the site you gave (the man page it seems?):

I'm having trouble take the words after just -a or just -b and being able to manipulate them as needed (parsing, seeing if readable). Right now I figured a simple echo of the string after the -a or -b would be a good start, at least then I know which var to play with.

while getopts ab: name
do
    case $name in
        a) bval="$OPTARG"
            echo $name $OPTARG;;
        b) echo hello
    esac
done

Also thanks for the reply.

First, you need to remember that your instructor has given you a very simple script. If everything was this simple there would be no getopts. But getopts can handle almost anything you throw at it and with more complex scripts it would be a nightmare to do it all manually.

Now in your case, it looks like "alert user" is the basic command. You have to give some user name. A command like just "alert" all by itself makes no sense. It may be that your script only handles one user... that's enough as an example to learn scripting. But in the real world it would probably be able to handle multiple users "alert user1 user2 user3 user4....". So far we have not talked about options.

You have a option called -m and it seems to require an argument. Ditto -w. So it's like this...


alert user1
alert  -m "some -m stuff" user1
alert  -m "some -m stuff" -w "some -w stuff" user1

getopts is going to handle the stuff in color. The reason getopts is in a while loop is that it is looping through the command line, word by word, left to right. After you finish absorbing the command line you should know what to do. If that involves another while loop, then you will need a second while loop.