Home CPSC 225

Assignment 5: Shell Scripting



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.



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 the traditional UNIX command fortune which is installed on the CPSC server. Of course we are writing our own, but you can run 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:

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

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

ifinlay@cpsc:~$ 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:

ifinlay@cpsc:~$ 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:

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

ifinlay@cpsc:~$ 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.



  1. Start by downloading the .fortunes.txt file to your home directory if you haven't already.
  2. Then, open up a file called "fortune.sh" in Vim. This will be your script file where you will write commands to solve the problem.

  3. Start the script by checking 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. If the -f flag is not given, the file should be the default ~/.fortunes.txt.
  4. Do the same thing for the "-a" flag. If it is present, set a variable and then shift past it. Note that the -a will be in $1 even if -f was supplied - because of the calls to shift.
  5. 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.
  6. Next, create a loop that executes that many times (or only once if the user did not supply the "-n" flag).
  7. 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.
  8. 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!



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

submit 5 fortune.sh

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 © 2024 Ian Finlayson | Licensed under a Attribution-NonCommercial 4.0 International License.