CS197U: A Hands-on Introduction to Unix

Assignment 4 : Due Thursday, October 15th at 3:45 pm

This assignment will teach you basic shell and awk scripting (see Lectures 5 and 6). It will also require using some of the earlier commands you've learned.

Part 1: Writing Bash Scripts (2.5 pts)

1. Make a new directory called 'assign-4' in /courses/cs100/cs197u/USERNAME. Make sure to replace USERNAME with your Edlab id.

2. Copy the assignment files from /courses/cs100/cs197u/cs197u/assignments/assign-4 into your 'assign-4' directory. Change to your assign-4 directory.

3. Edit the script.sh file to make it print out the name of each file in the data directory using a for loop.

4. Run your script. The output should look something like:

elnux7> ./script.sh
data-1.txt
data-2.txt
data-3.txt

Note: (1) The script should not have the hardcoded names of the data files. Instead, the script should figure out what files are in the data/ directory on its own.
(2)It is also fine if it prints out something like "data/data-1.txt" as the name of each file.


Part 2: AWK scripts (2.5 pts)

1. Edit the avg.awk file so that it will print out the average for the second column of the input file.

2. Run your script. Verify that the output of your script is correct:

elnux7> awk -f avg.awk data/data-2.txt
Average: 1204848
elnux7> awk -f avg.awk data/data-3.txt
Average: 16108.6

Part 3: Combining Bash and AWK (2.5 pts)

1. Make a new copy of your script.sh and call it script2.sh.

2. Modify script2.sh and make it so that after printing out a filename, it will call the awk script on that file.

3. Run the script. As a result, you should get output like:

data-1.txt
Average: 30
data-2.txt
Average: 1204848
data-3.txt
Average: 16108.6

3. Save the output into a file named output.txt

Part 4: Combining Bash, AWK, and other commands (2.5 pts)

1. Make a new copy of your script2.sh and call it script3.sh.

2. Modify script3.sh and make it so that after printing out a filename and the output of the awk script on that file, it will rename the file into "mydata-[number].txt" instead of "data-[number].txt" (current format). Print out the new file name right after the average output.

3. Run the script. As a result, you should get output like:

data-1.txt
Average: 30
data-1.txt renamed to mydata-1.txt
data-2.txt
Average: 1204848
data-2.txt renamed to mydata-2.txt
data-3.txt
Average: 16108.6
data-3.txt renamed to mydata-3.txt

3. Save the output into a file named output2.txt

Bonus Part 5: Script Gotchas (2.5 pts)

1. Fix the errors in badscript.bash such that the output only contains 4 lines and looks something like:

Hi there 197U Student!
Right
Right
Hi tian! Today is Sun Oct 4 11:12:46 EDT 2015

Note: Follow the instructions inside the badscript.sh.

Submission

I will collect all these files directly from /courses/cs100/cs197u/USERNAME/assign-4/:

Make sure you followed the directions in all parts so the files exist in the correct location and are named correctly as above. Please verify the paths by using cd and ls as in Assignment 2.