Skip to main content.

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.

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:

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)