Home‎ > ‎CIS 98‎ > ‎

Lab 6 - Escaping and Quoting

In this lab you'll expand on the program from the last lab by having your lookup script take arguments and find more information about the user.

Script Arguments

Scripts are more useful when you can change their behavior by supplying arguments on the command line. Special variables inside your script represent command line arguments. For example, if I call a script like this:

$ ./example Mike 

The word "Mike" is passed in as the first argument. In my script I reference that argument with the $1 special variable. Like this:

#! /bin/bash
# This is example
echo My first argument is $1
echo My second argument is $2
echo All of my arguments are $@

Create the example script on your own and try it out. What happens when you give it the following arguments: 

$ ./example Mike
$ ./example Mike Matera
$ ./example $USER
$ ./example '$USER'

Be sure you understand each output. 

Update Lookup

Your lookup script from the previous lab could only lookup the current user. Update it to take a single argument: The real name of a user. For example:

$ ./lookup "Mike Matera"

Notice the quotes around the name. Your script should produce output like the following: 

$ ./lookup "Mike Matera"
Mike Matera
User ID: 205
Primary Group: staff
Home Directory: /home/mmatera
Login Shell: /bin/bash

  1. The script looks up the person who is named on the command line.
  2. If multiple names match the input you must lookup the first name in /etc/passwd (hint: use the head command)
  3. The "Primary Group" value is now the name of the group, not the number. This information is found in /etc/group
Your output should look like mine when your script is run with my name. Be sure to test your script with different input so you can be sure it's correct. 

Handle Common Problems

Your lookup script should behave well given the unreliable nature of human input. It's impossible to be ready for every kind of error but you should check your script for the following two scenarios.

Incomplete Input

A user can ask your script for a partial name. Your script should find the first user that matches that partial name and print that user's information. Here's an example:

$ ./lookup Jeff
Jeffrey Bergamini
User ID: 206
Primary Group: jeff
Home Direcory: /home/jeff
Login Shell: /bin/bash

Here's another example using a partial name:

$ ./lookup Mat
Mike Matera
User ID: 205
Primary Group: staff
Home Direcory: /home/mmatera
Login Shell: /bin/bash

In both cases the the partial full name matched an existing user and that user's data was properly returned. 

Incorrect Input (Extra Credit)

Your lookup script might be used to lookup the name of someone who doesn't exist on the system. In that case your script should produce no output and exit. For example: 

$ ./lookup "Santa Clause"

Your script must not show errors or hang forcing the user to stop the program with CTRL-C. Here are some hints:
  1. You can make a decision in a script using && and ||
  2. Grep can return "true" or "false" just like next_to_ring does.  

Turn In


  • 15 points for proper execution
  • 5 points for the design and style of the script
  • 5 points extra credit for handling incorrect input