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 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.
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
The order of the arguments is fixed, however. If present the
option is always first, then the
-a option and finally the
You also don't need to make sure that the
-n arguments have values. You can assume that if someone supplies
those, they will put in the file name and number correctly.
-foption. 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
shiftcommand 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.
readcommand, and append their input to the end of the file. You do this by using output redirection as discussed here.
shufcommand which randomly shuffles its input, together with the
headcommand 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 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 © 2019 Ian Finlayson | Licensed under a Creative Commons Attribution 4.0 International License.