COMP6442 - lab09 - CECS - ANU Skip navigation Computer Science Courses ANU College of Engineering & Computer Science EXPLORE ANU » Home About Study Research Giving Alumni Current students Staff News Events Maps Library Jobs ANU College of Arts & Social Sciences ANU College of Asia & the Pacific ANU College of Business & Economics ANU College of Engineering & Computer Science ANU College of Law ANU College of Medicine, Biology & Environment ANU College of Physical & Mathematical Sciences » more academic areas A-Z INDEX » A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Search query Search button Computer Science Courses ANU College of Engineering & Computer Science Computer Science Courses School of Computer Science Current students Courses list COMP6442 Home Schedule Lecture Notes Assignments Labs/Tutorials Old Exams Marks and Lab registration Announcements Discussion People Related sites Wattle Forums ANU College of Engineering & Computer Science Current students (ANU) COMP 2100/2500/6442: Lab 8 - Design Patterns, Bash Pre-Lab Tasks [2 marks] The first 10 minutes of the lab session time may be spent by your tutor to mark pre-lab tasks. Bash basics Revise the lecture material on bash Write a bash script that outputs "Hello World" Write a bash script that outputs a greeting which is different for different names. Provide the name as a parameter. An example of how you would interact with your script would be: % ./greeting Eric
Hello Eric
% ./greeting Alistair
Greetings Professor Rendell
% ./greeting Anish
G'Day Anish You only need to have a few different greetings for different names and one default greeting. Please keep all your greetings positive and friendly. String Counting with Java This program must be completed in order to start the in-class activities. Write a Java program called, StringCount that takes two string arguments from the command line and returns a count of how many times an occurrence of the first string can be found in the second $ java StringCount "aaa" "aaaabbb"
2 Occurrences must be contiguous. For example, ‘cat’ does not occur in ‘cxaxt’ Occurrences may overlap. For example ‘wowow’ occurs twice in ‘wowowow’ (Hint - if the expression i == mainString.indexOf(subString, i) evaluates to true then the subString will be within the mainString starting at position i.) Using the Facade Design Pattern Extend the StringCount program to support loading files and counting occrrences of a string within the a file $ echo "aaabbbaaa" >> TestFile
$ java StringCount -f "TestFile" "aaa"
2 Use the facade design pattern to implement the file load function In Class Activity - Writing a Test Harness in Bash [2 marks] The objective is to write a Bash script to test the StringCount program with different string combinations. This may be done individually or in a small group of 2 or 3 students. Create a file called 'cases' to contain the test cases. Each line should contain two strings. Here are a few lines to get you started. Add some more of your own x x
x xx
wowow wowowowow The following simple shell script will read words from each line of its input and print those words out again #!/bin/bash
while read x y
do
echo ${x} ${y}
done The key feature of this script is the read command which reads a line of input and assigns the first word to the variable x and the rest of the line to the variable y. The read command gives an exit code of 0 (= success = True) if was was able to read the words, or 1 (= failure = False) if it encounters the end of its input. That makes it perfect for use in a while loop, as seen in this example. When the input runs out, it returns 1, and the loop terminates. Save this script in a file called tester Make it executable, and run it over your test input by typing ./tester < cases Modify tester so that for each input line of the form string1 string2, it outputs the number of occurrences of string1 that StringCount claims are in string2. Note: This does not mean rewrite the Java program as a bash script. It means write a bash script that runs the Java program on each line of its input and formats the results. Modify your cases file so that each line now contains 3 things, the two words as before, plus the correct number of occurrences of the first word in the second. For example, your file will start: x x 1
x xx 2
wowow wowowowow 3 Modify your tester script so that for each test it prints a line of the following form: Test i: string1 string2: Expected n, got m where i is the line number of this test, n is the correct number of occurrences of string1 in string2, and m is the number found by StringCount. Modify your tester so that when strcnt gets the right answer, it prints a line of form: Test i: Passed The key to this is to use an if statement in your shell script. Remember that the form of an if statement is as follows: if command-list
then
command-list
else
command-list
fi Note: The line breaks (or semicolons in their place) are required. You can leave out the else part if you don't need it. Have a look at the manual page for the test command (by typing man test). That should tell you everything you need to know now to complete this exercise. The test command evaluates a boolean expression (as well as being able to test for all sorts of other conditions). It basically lets you program in Bash in a way very like what you would do in Java. The test command is so commonly used in shell scripts that it has a short-hand notation in which [ stuff ] means the same as test stuff (those spaces inside the square brackets are required). Use this notation to simplify your solution. Updated: 15 May 2016 / Responsible Officer: JavaScript must be enabled to display this email address. / Page Contact: JavaScript must be enabled to display this email address. / Powered by: Snorkel 1.4 Contact ANU Campus Map Copyright Disclaimer Privacy Freedom of Information +61 2 6125 5111 The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906