Home CPSC 225

Assignment 5: Shell Scripting

Objective

To gain experience writing shell scripts employing variables, arguments, if statements and loops.

You should have completed week fourteen before attempting to complete this assignment.


Overview

For this assignment you will write a simple fortune teller script. When called without arguments, the script will print a random fortune, read from a text file. The script will take options to allow you to specify which text file to read from, to add a new fortune instead of reading an existing one, and to request more than one fortune.

You can use the .fortunes.txt file to test your program. This file contains 115 randomly selected fortunes. Note that this file starts with a '.', so it is hidden file and will not be shown by ls by default. Place this file in your home directory.

This script is based on a traditional Unix command which can be installed on your VM from the package fortune-mod and then run as fortune. Of course we are writing our own, but you can look at the original, if you like.


Example Runs

This section shows some example runs of the script. The first shows the default behavior which is to select a random line from the .fortunes.txt file:

finlaysoni@myvm:~$ fortune.sh
It takes courage to admit fault.
finlaysoni@myvm:~$ fortune.sh
Every wise man started out by asking many questions.
finlaysoni@myvm:~$ fortune.sh
A fresh start will put you on your way.

Next we can specify a different fortunes file by using the -f flag:

finlaysoni@myvm:~$ fortune.sh -f test.txt
test 6

(Here there is a file called test.txt which contains the line "test 6".)

We can also specify that we are adding a fortune to the list instead of printing one:

finlaysoni@myvm:~$ fortune.sh -a
Enter a new fortune: You will run afoul of a badger this week.

This command should add the users fortune into the fortune file — either the default one or the one the user specified.

The last optional argument is -n with a number which specifies how many fortunes to display or add:

finlaysoni@myvm:~$ fortune.sh -n 3
Place special emphasis on old friendship.
Welcome change.
Feeding a cow with roses does not get extra appreciation.

finlaysoni@myvm:~$ fortune.sh -a -n 2
Enter a new fortune: Avoid swimming today.
Enter a new fortune: Now is a good time to invest in textiles.

All three optional arguments can be present. So for example, the command "fortune.sh -f test.txt -a -n 5" will add 5 fortunes to the file test.txt.

The order of the arguments is fixed, however. If present the -f option is always first, then the -a option and finally the -n option.

You also don't need to make sure that the -f and -n arguments have values. You can assume that if someone supplies those, they will put in the file name and number correctly.


Details

  1. Start by downloading the .fortunes.txt file to your home directory if you haven't already.
  2. Check for the -f option. Because it must be first, you can simply check if $1 is equal to "-f". If so, capture the name of the file into a variable. Then run the shift command twice. This will serve to skip over the -f and file name so that the next argument past that will appear as $1. You can brush up on this command here.
  3. Do the same thing for the "-a" flag. If it is present, set a variable and then shift past it.
  4. Do the same thing for the "-n" flag, saving the number of fortunes the user wishes to see or add, if the flag is there.
  5. Next, create a loop that executes that many times (or only once if the user did not supply the "-n" flag).
  6. Inside the loop check if they are adding a fortune. If so, ask them to enter a fortune with the read command, and append their input to the end of the file. You do this by using output redirection as discussed here.
  7. If they are not adding a fortune, then we need to show them one instead. (This is still in the loop). To show them a random fortune, use the shuf command which randomly shuffles its input, together with the head command which can select only the first line(s) of its input, joined with a pipe. You can refresh yourself on pipes here.

Be sure to test your script thoroughly to be sure that it works in all cases we talked about!


Submitting

When you are satisfied that your script is complete, submit your results with the following command:

./submit username 5 fortune.sh

Where username is your UMW netid.

This will transfer your submission to the server where it will be graded. It will print your grade to the screen and record your grade. You can submit the assignment as many times as you like. Your best grade will be used.

Copyright © 2018 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.