Assign 3: Bash Scripts
Due: Monday before class
Goals for Assignment 3
After the assignment, you should know how to write basic bash
scripts, including using if
statements
and for
loops.
Objective: Writing Bash Scripts (100 pts)
Create an assign3
subdirectory within
your cs397/assignments
directory.
Converting Assignment 2 (10 pts)
Copy your regex.txt
file from the last
assignment into this directory and name
it regex.sh
. Convert the contents of the
file into a shell script.
As with all scripting assignments, comment your code as appropriate. At the very least, your name and a high-level description of the script should be at the top of the file.
Using the script
command, demonstrate your shell
script in a file called regex.out
Executing Scripts (20 pts)
Copy the (whole)
directory /csdept/courses/cs397/handouts/bash/
into your assign3
directory.
Write a script called execAll.sh
that
uses a for
loop to get the files from
the bash
directory and then executes each
script.
Demonstrate your script in a file called execAll.out
Have I Got a Case for You! (20 pts)
bash scripts tend to be practical. This is not practical.
Create a script called legs.sh
that prompts the user
for the name of an animal and tells the user how many legs an animal
has.
- If it's a spider or an octopus, it has 8 legs (okay, tentacles, but whatever)
- If it's a horse or a dog, it has 4 legs.
- If it's a kangaroo or a person, it has two legs
- If it's any kind of cat or any kind of cow, it has four legs.
- Otherwise, the animal has an unknown number of legs.
Write the script using as little code as possible.
Demonstrate your Bash script in a script
named legs.out
.
Example Output
$ bash legs.sh Enter an animal: kangaroo The kangaroo has two legs $ bash legs.sh Enter an animal: spider The spider has eight legs $ bash legs.sh Enter an animal: octopus The octopus has eight legs $ bash legs.sh Enter an animal: dog The dog has four legs $ bash legs.sh Enter an animal: ostrich The ostrich has an unknown number of legs $ bash legs.sh Enter an animal: cat The cat has four legs $ bash legs.sh Enter an animal: alley cat The alley cat has four legs $ bash legs.sh Enter an animal: tom cat The tom cat has four legs $ bash legs.sh Enter an animal: Jersey cow The Jersey cow has four legs
Script to Submit Your Assignment (50)
You need to remember the command to submit your assignment every time. So, let's automate it!
Use your development skills to build the script in pieces to make sure it's working correctly.
Script Requirements:
- The script should be called
submit397.sh
- The script should take a command-line argument that is the name of the assignment.
- The script should copy your assignment directory (likely located
in ~/cs397/assignments/assignmentname) to your
turnin
directory. - The script should use your environment variable
$CS397
- If you previously submitted the assignment into
the
turnin
directory, the script should copy the old assignment into a backup copy in the turnin directory, with the time in its name to keep track of versions of the submission. Then, copy the current version of the assignment into theturnin
directory. - The submission (directory and contents)'s group should
be
cs397
, and the permissions should be set such that the group has read and execute permissions (but not write) but others have no permissions on the directory. Note that this does not mean that others can access your assignment because the$CS397/turnin/username
directory's group is turnin; I am the only member of that group. - Upon submission (read: copy), show the contents of
the
turnin
directory to show that the submission worked. - The script should handle errors and display appropriate messages
to the user, including:
- incorrect number of arguments
- the assignment directory to copy doesn't exist
Clean up (read: remove) all your extra copies of submitted assignments that you created during testing.
Using the script command, demonstrate your shell script in a file
called submit.out
Finishing up: What to turn in for this assignment
Use your submit397.sh
script to turnin your assignment
into the turnin directory.
Grading (100 pts)
- 100 pts: Correct scripts, well-documented, demonstrated.