COP 2344 (Shell Scripting) Project #8
Parsing Command Line Arguments with getopts

 

Due: by the start of class on the date shown on the syllabus

Description:

Interactive shell scripts are useful, but to be scriptable a command needs to be able to use command line arguments to accomplish tasks in a non-interactive manner.  A truly versatile script will be able to work either way.

While there are many ways to handle command line arguments, *nix systems generally use command line options and arguments (also called parameters) the same way.  This has led to the inclusion of getopts as a standard utility to make this tasks simpler, known as parsing the command line arguments.  (A version of getopts is also available for C, Perl, and other languages.)  The Gnu project provides a similar tool, getopt with many additional features compared to standard getopts.

Requirements:

For this project you will create a shell script to manage a to do list.  This is a list of tasks you need to accomplish soon.  Using your script users must be able to: view the list of tasks, add an item to the list, and remove an item from the list.  Additionally you should provide a help option to provide instructions on how to use your script.

When displaying the list of to-do tasks, the output should number them.  Those numbers will be used to remove an item by its number.

The list should be kept in a private file in the user's home directory named $HOME/.todo.  The file format should be simple: one item per line.  (Note you don't store any line numbers in the file itself, just the to-do item as entered by the user.)

For extra credit provide a menu driven user interface too.  When invoked with no command line arguments, your script should provide this interactive mode.

You can make up your own set of options, but I suggest these for the required three tasks:

You must use either getopts (the POSIX standard tool) or getopt (the Gnu tool).  Personally I prefer the Gnu getopt tool, so I can have more control over error messages and also to provide for long versions of options.  (e.g., For a help option you might provide both -h and --help.)

Additional Notes:

You may use Gnu and Bash extensions (e.g., getopt, $RANDOM, mktemp, and select).

Please review the Temporary file tutorial, getopt, and other resources from our class web page.  (Additional getopt examples can be found at /usr/share/doc/util-linux-* on YborStudent.)

In your script, before doing anything interactive check if any command line options were present.  Use an if statement and test command for this.  If there are no options then draw the menu and read the user's input (interactive stuff).  On the other hand, if command line options were used, then don't do that.  Just process the command line arguments.

If command line options are present you can use similar shell statements to the getopt examples on the class website.  The idea is to normalize the command line arguments to a standard format that is easy to examine.  You then examine the normalized arguments one at a time.  The easy (and common) way to do this is use a loop, and set environment variables for each option present.  Later you can easily use tests such as:
if [ "$ADD_OPT" = 1 ]; then ...; fi

Sample session using the model solution

/home/wpollock> todo -l

  1.  Pickup pet weasel
  2.  Install weasel door
  3.  Buy some Purina weasel chow
  4.  Get weasel "fixed"
  5.  Sell weasel puppies
  6.  Finish Shell Script project

/home/wpollock> todo
1) Add
2) Remove
3) View
4) Help
Enter number from menu (q to quit, hit enter to redraw menu): 1
(Add)    Enter new task to add (hit enter to cancel): Post project to web site

         List Updated:

  1.  Pickup pet weasel
  2.  Install weasel door
  3.  Buy some Purina weasel chow
  4.  Get weasel "fixed"
  5.  Sell weasel puppies
  6.  Finish Shell Script project
  7.  Post project to web site

Enter number from menu (q to quit, hit enter to redraw menu): 2

  1.  Pickup pet weasel
  2.  Install weasel door
  3.  Buy some Purina weasel chow
  4.  Get weasel "fixed"
  5.  Sell weasel puppies
  6.  Finish Shell Script project
  7.  Post project to web site

(Remove) Enter task number to remove (hit enter or "q" to cancel): 8
        *** You must enter a number between 1 and 7! ***

  1.  Pickup pet weasel
  2.  Install weasel door
  3.  Buy some Purina weasel chow
  4.  Get weasel "fixed"
  5.  Sell weasel puppies
  6.  Finish Shell Script project
  7.  Post project to web site

(Remove) Enter task number to remove (hit enter or "q" to cancel): 6

         List Updated:

  1.  Pickup pet weasel
  2.  Install weasel door
  3.  Buy some Purina weasel chow
  4.  Get weasel "fixed"
  5.  Sell weasel puppies
  6.  Post project to web site

Enter number from menu (q to quit, hit enter to redraw menu):
1) Add
2) Remove
3) View
4) Help
Enter number from menu (q to quit, hit enter to redraw menu): help

    USAGE:
       todo [ -d ] [ -l ] [ -a TASK | -r NUM ]
       todo [ -d ] [ -h | -v ]
     Options:
        -d, --debug       Turn on debugging mode.
        -a, --add="TASK"  Add the TASK string to the end of the list.
        -r, --remove=NUM  Remove the NUM-th task from the list.
        -l,--list         Display list of to-do tasks to standard output.
        -h, --help        Display this help message.
        -v, --version     Show author and version information.

    If the -l option is combined with either -a or -r, the modified list
    will be displayed.

Enter number from menu (q to quit, hit enter to redraw menu): q
/home/wpollock> 

To be turned in:

A copy of your script.

You can send as email to .  Please see your syllabus for more information about submitting projects.