This assignment has a little bit of bash, and then some very basic python programs.
For the bash problems in this assignment, students are limited to using the following commands:
pwd
ls
cd
touch
rm
rmdir
find
echo
cat
sort
grep
head
tail
uniq
rev
cut
sed
awk
date
if
/else
for
while
read
wc
You can also use any other bash feature discussed in the lecture notes.
When grading your assignments, each problem will be thoroughly tested with many types of input, so make sure you test your scripts well!
Obviously, this homework requires the use of the bash shell. If you do not have a Mac, Unix, or Linux computer that can run bash, do this assignment in one of the CS computer labs in Gould-Simpson.
All of your scripts should be well-formatted and easy for the graders to read. Each script should have a header comment at the top (under the Sha-Bang, in the case of the bash scripts) that has the following format:
#
# Author: Student Name
# Description:
# A description of what this program/script does!
# In this section, you can document how the script works, and what the
# command-line options are.
#
If any part of your scripts are particularly complex, you should put documentation comments above those lines of code.
Write a bash script named todo.sh
.
This script will act as a very basic todo-list management tool.
This script will have four different modes of operation: list
, add
, remove
, and clear
.
The mode will always be given as the first argument of the script.
This script will use a file called todo.txt
as a “database” to store the todo-list information in.
For all of the operations that this script does, assume that todo.txt
is in the current working directory.
If an invalid mode is specified on the command line, you should print the message:
Unknown mode
And then exit the script.
At the beginning of the script, before doing any of the list operations, you should check to ensure that ./todo.txt
exists (for all modes).
If it does not exist, print:
The todo-list database file is missing!
And then immediately exit
from the script.
If it does exist, continue execution as normal.
In list
mode, the current todo-list will be shown.
So, if todo.txt
is empty, running the script in list
mode will look like:
$ ./todo.sh list
$
If todo.txt
has some todo-list entries in it, running would look like:
$ ./todo.sh list
* Check the mail
* Finish assignment 3
* Be awesome
Each line of todo.txt
represents an entry in the todo-list.
For the example run of ./todo.sh list
above, todo.txt
would have had the following contents:
Check the mail
Finish assignment 3
Be awesome
Essentially, list
mode iterates over each line of todo.txt
, and prints the line to stdout with a *
and a space before it.
The *
character represents list bullet points.
Because of the way this script works, there can be no multi-line todo-list entries.
In add
mode, this script will add todo-list entries.
It will add an entry to the to-do list by appending the entry to the file named todo.txt.
Say todo.txt
currently looks like:
Check the mail
Finish assignment 3
Be awesome
And running in list
mode displays:
$ ./todo.sh list
* Check the mail
* Finish assignment 3
* Be awesome
We can add an element to the list by running:
$ ./todo.sh add "Take out the trash"
Todo entry 'Take out the trash' added.
$
Notice that the first argument to the script is the mode (add
) and the second argument is a string with the todo-list entry to add.
A successful run in add
mode prints out a message of the form shown above, and it should add an element to the list.
Now, todo.txt
will look like:
Check the mail
Finish assignment 3
Be awesome
Take out the trash
And running in list mode after this will display the list, including the new entry:
$ ./todo.sh list
* Check the mail
* Finish assignment 3
* Be awesome
* Take out the trash
In remove
mode, todo.sh
will remove an individual entry from the todo list file.
Continuing from the last example, say list
mode displays:
$ ./todo.sh list
* Check the mail
* Finish assignment 3
* Be awesome
* Take out the trash
Perhaps we have now completed the task of “Be Awesome,” so we want to remove it from the list.
To do the removal, run todo.sh
in remove
mode and give it an entry number to remove as a command line argument.
In this case, “Be awesome” is entry 3, because it is the third line printed.
So, to do the removal:
$ ./todo.sh remove 3
Todo entry 3 removed.
$
If all is successful, remove
mode should print a message with the form shown above.
And now list
displays:
$ ./todo.sh list
* Check the mail
* Finish assignment 3
* Take out the trash
The * Be awesome
entry has been removed.
Perhaps we have also checked the mail, so we want to remove the first entry.
$ ./todo.sh remove 1
Todo entry 1 removed.
$
$ ./todo.sh list
* Finish assignment 3
* Take out the trash
The * Check the mail
entry has been removed.
You do not have to worry about a user of the script typing in an invalid row to remove, we won’t test your program in that way.
HINT: to remove an arbitrary line from a text file, you can use the command sed -i '' "Nd" file-name
.
Just replace N
with the line number you want removed, and replace file-name
with the name of the file to do the removal on.
Running the script in clear
mode clears the todo-list. If list-mode displays:
$ ./todo.sh list
* Feed the dog
* Feed the fish
* Feed the cat
And then we run the script in clear
mode:
$ ./todo.sh clear
All todo entries cleared.
$
An informative message is printed, and now the todo-list is empty:
$ ./todo.sh list
$
You should use one or more bash conditionals and at least one bash loop when implementing todo.sh
.
Write a python program named star-wars.py
.
This program has one job.
When executed, it will print out the following snippet from the “Star Wars: A New Hope” script to stdout:
*** A large, multiple-eyed Creature gives Luke a rough shove. ***
CREATURE: Negola dewaghi wooldugger?!?
*** The hideous freak is obviously drunk. Luke tries to ignore ***
*** the creature and turns back on his drink. A short, grubby ***
*** Human and an even smaller rodent-like beast join the ***
*** belligerent monstrosity. ***
HUMAN: He doesn't like you.
LUKE: I'm sorry.
HUMAN: I don't like you either.
*** The big creature is getting agitated and yells out some ***
*** unintelligible gibberish at the now rather nervous, young ***
*** adventurer. ***
HUMAN: Don't insult us.
You just watch yourself.
We're wanted men.
I have the death sentence in twelve systems.
LUKE: I'll be careful then.
HUMAN: You'll be dead!
Write a python program named 007.py
.
This program has one job.
When executed, it will print out the following text art to stdout:
________ ________ _________ ____________;_
- ______ \ - ______ \ / _____ //. . ._______/
/ / / // / / //_/ / // ___ /
/ / / // / / / .-'//_/|_/,-'
/ / / // / / / .-'.-'
/ / / // / / / / /
/ / / // / / / / /
/ /_____/ // /_____/ / / /
\________- \________- /_/
This problem will be graded out of 100 points.
This was assigned on Thursday, February 2, 2017. It is due Tuesday, February 9, 2017, at 12:30pm (The start of class).
Turn-in instructions:
.sh
and .py
file in a directory named netid-assignment-3
where netid
is your netid. Make sure only the .sh
and .py
files exist in this directory, no others.netid-assignment-3
directory. The result should be named netid-assignment-3.zip
. There are instructions online for how to do this with a Mac and how to do this with a PC. Make sure to zip the directory, not the files individually!netid-assignment-3.zip
to the assignment-3 D2L dropbox.Following these turn-in instructions closely is very important, because our grading scripts will depend on some of the details. You may lose points if these instructions are not followed precisely!