Understanding Getopts in an otherwise easy assignment

Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Understanding Getopts in an otherwise easy assignment
# 1  
Old 03-15-2014
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;
    case $option in
            echo recieved -a
            echo recieved -f
            echo recieved -z
            echo "invalid option -$OPTARG"

2. Relevant commands, code, scripts, algorithms:

Mainly getopts is encouraged to be used.

3. 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.

4. 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:
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).
# 2  
Old 03-15-2014
I don't really understand your question. But there are plenty of getopts examples that have been posted to this site. This thread: https://www.unix.com/showthread.php?t=3619 has a script I wrote a while back that uses getopts. Does that help?
# 3  
Old 03-15-2014
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?
# 4  
Old 03-16-2014
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
    case $name in
        a) bval="$OPTARG"
            echo $name $OPTARG;;
        b) echo hello

Also thanks for the reply.

Last edited by ByronicX; 03-16-2014 at 11:27 AM..
# 5  
Old 03-16-2014
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Need a little help with assignment

Hello all im currently working on this assignment and a little stump on how to check for an argument heres the instructions: Step 4: Modify your script so that if there is an argument called TestError you display the following error message with your usage statement. TestError found Example:... (1 Reply)
Discussion started by: bsn3971
1 Replies

2. Homework & Coursework Questions

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory By modification time when called with lm By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a shell script which... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

3. Shell Programming and Scripting

Help Assignment !! :D

Q-1 Write a shell script in Unix that lists files from your current working directory By modification time when called with lm By access time when called with la. By default, the script should show the listing of all the files in the current directory. Q-2 Write a... (1 Reply)
Discussion started by: Vishank Parikh
1 Replies

4. Homework & Coursework Questions

Assignment Help

1. List commands to create the directory hierarchy $HOME/a/b/c in vi to replace all occurences of TMP with tmp in lines 1 through 10 in vi to replace first occurence of CPU_file with DISK_file at line 15 2. Explain with a very simple example, usage of "ls -a" 3. What do the... (2 Replies)
Discussion started by: jessesaini
2 Replies

5. Shell Programming and Scripting

RE value assignment

Hi all How do I assign a pattern to a variable after a match is found using a regular expression in PERL? For example using a regular expression (RE) and matching as given if ($_ =~ /(?:\s*+\s*,)*\s*+\s*/) I want to assign the pattern matched by the RE to a variable. e.g. given the... (4 Replies)
Discussion started by: my_Perl
4 Replies

6. Programming

Help understanding pointer assignment in c

after some years of pause, im returning to c. char *varname = "asd"; int *number = 4; the above code is wrong, because its assigning a value to an unreserved space, or because its changing the address the pointer is pointing ? thanks for the replys!! (3 Replies)
Discussion started by: broli
3 Replies

7. Shell Programming and Scripting

using getopts

Hi, I have a program where I want to use getopts. I want to use "-i" option and then optionally supply arguments. If user dosent supply arguments, then also it should work. Please tell me how to proceed. Here is some code, this is not right code btw but a sample to understand what I want to... (1 Reply)
Discussion started by: som.nitk
1 Replies

8. What is on Your Mind?

New Assignment

All Sys Administrators, With due respect I would like to know what should be BEST Things to do when LEAVING one job , and what Precaution MUST be taken while taking over new JOB?? Please Discuss in detail the STEP to be taken for both the TIME ?? (3 Replies)
Discussion started by: vakharia Mahesh
3 Replies

9. UNIX for Dummies Questions & Answers

Need help on installing an EASY to use and easy to install command line text editor

Hi again. Sorry if it seems like I'm spamming the boards a bit, but I figured I might as well ask all the questions I need answers to at once, and hopefully at least get some. I have installed Solaris 10 on a server. The default text editors are there (vi, ex, ed, maybe others, I know emacs is... (4 Replies)
Discussion started by: EugeneG
4 Replies

10. UNIX for Dummies Questions & Answers

how to do my assignment?????

my lecturer want to do an assignment about telnet server..first, he want us to download the telnet server..then he want we do about copy files and share files by using the telnet server....what is that??? and one more thing is how to do 'ping'???? (1 Reply)
Discussion started by: amelia
1 Replies
Login or Register to Ask a Question