Before the lab you should re-read the relevant lecture slides and their accompanying examples.
Create a new directory for this lab called lab02, change to this directory, and fetch the provided code for this week by running these commands:
$ mkdir lab02
$ cd lab02
$ 2041 fetch lab02
Or, if youre not working on CSE, you can download the provided code as a zip file or a tar file.
There is a template file named counting_classes_answers.txt which you must use to enter the answers for this exercise.
Download counting_classes_answers.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/counting_classes/counting_classes_answers.txt .
The autotest scripts depend on the format of counting_classes_answers.txt so just add your answers dont otherwise change the file. In other words edit counting_classes_answers.txt:
gedit counting_classes_answers.txt &
The file classes.txt contains a list of CSE classes downloaded from myUNSW.
Download classes.txt, or copy it to your CSE account using the following command:
$ cp -n /web/cs2041/20T2/activities/counting_classes/classes.txt .
. Write a shell pipeline to print how many classes there are. Hint: the output of the pipeline should be:
441
. Write a shell pipeline to print how many different courses have classes. Hint: cut with the -f option will be useful here.
Hint: the output of the pipeline should be:
35
. Write a shell pipeline which will print the course with the most classes (and no other courses) and how many classes are in this course.
Hint: the output of the pipeline should be:
31 COMP1521
. Write a shell pipeline that prints the room most frequently-used room by CSE classes and how often it is used. Dont include the CSE lab rooms.
Hint: the output of the pipeline should be:
26 Quad 1042
. Write a shell pipeline that prints the most common day and hour in the week for classes to start and how many classes start at that time.
Hint: cut has a -c option.
Hint: the output of the pipeline should be:
20 Fri 11
. Challenge: Write a shell pipeline that prints a list of the course codes (only) of COMP courses that run 2 or more classes of the same type starting at the same time on the same day (e.g. three tut-labs starting Monday at 10 00). Hint: this should be the output of your pipeline:
COMP1000
COMP1511
COMP1521
COMP2041
COMP2511
COMP2521
COMP3331
COMP6441
COMP6841
COMP9044
COMP9311
COMP9313
COMP9331
COMP9417
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest counting_classes
Autotest Results
77% of 572 students who have autotested counting_classes_answers.txt so far, passed all autotest tests. 99% passed test q1 q2 q3 q4
98% passed test q5
80% passed test q6
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab02_counting_classes counting_classes_answers.txt
before Tuesday 16 June 17 59 to obtain the marks for this lab exercise.
Write a program digits.sh that reads from standard input and writes to standard output mapping all digit characters whose values are less than 5 into the character < and all digit characters whose values are greater than 5 into the character >. The digit character 5 should be left unchanged.
Sample Input Data | Corresponding Output | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
Hint: tr can be used.
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest digits
Autotest Results
99% of 557 students who have autotested digits.sh so far, passed all autotest tests. 99% passed test 1 2 3 4 5
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab02_digits digits.sh
before Tuesday 16 June 17 59 to obtain the marks for this lab exercise.
Write a shell script called echon.sh which given exactly two arguments, an integer n and a string, prints the string n times. For example:
./echon.sh 5 hello hello hello hello hello hello./echon.sh 0 nothing ./echon.sh 1 goodbye goodbye |
Your script should print exactly the error message below if it is not given exactly 2 arguments:
./echon.sh
Usage: ./echon.sh <number of lines> <string> ./echon.sh 1 2 3
Usage: ./echon.sh <number of lines> <string>
Also get your script to print this error message if its first argument isnt a non-negative integer:
./echon.sh hello world
./echon.sh: argument 1 must be a non-negative integer ./echon.sh -42 lines
./echon.sh: argument 1 must be a non-negative integer
Although its better practice to print your error messages to stderr its OK to print your error messages to stdout for this exercise.
Hint: youll need to use the shell if, while and exit statements, shell arithmetic and the test command.
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest echon
Autotest Results
95% of 553 students who have autotested echon.sh so far, passed all autotest tests.
99% passed test 1
100% passed test 2
100% passed test 2
99% passed test 3 4 5 6
96% passed test 7
98% passed test 8
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab02_echon echon.sh
before Tuesday 16 June 17 59 to obtain the marks for this lab exercise.
Write a shell script file_sizes.sh which prints the names of the files in the current directory splitting them into three categories: small, medium-sized and large. A file is considered small if it contains less than 10 lines, medium-sized if contains less than 100 lines, otherwise it is considered large.
Your script should always print exactly three lines of output. Files should be listed in alphabetic order on each line. Your shell-script should match character-for-character the output shown in the example below. Notice the creation of a separate direcory for testing and the use of the script from the last question to produce test files. You could also produce test files manually using an editor.
mkdir test cd test ../echon.sh 5 text >a../echon.sh 505 text >bbb../echon.sh 17 text >cc../echon.sh 10 text >d../echon.sh 1000 text >e ../echon.sh 0 text >empty ls -l total 24-rw-rr 1 andrewt andrewt 25 Mar 24 10:37 a-rw-rr 1 andrewt andrewt 2525 Mar 24 10:37 bbb-rw-rr 1 andrewt andrewt 85 Mar 24 10:37 cc-rw-rr 1 andrewt andrewt 50 Mar 24 10:37 d-rw-rr 1 andrewt andrewt 5000 Mar 24 10:37 e-rw-rr 1 andrewt andrewt 0 Mar 24 10:37 empty../file_sizes.sh Small files: a emptyMedium-sized files: cc d Large files: bbb e rm cc d ../echon.sh 10000 . >lots_of_dots ls -l total 36-rw-rr 1 andrewt andrewt 25 Mar 24 10:37 a-rw-rr 1 andrewt andrewt 2525 Mar 24 10:37 bbb-rw-rr 1 andrewt andrewt 5000 Mar 24 10:37 e-rw-rr 1 andrewt andrewt 0 Mar 24 10:37 empty-rw-rr 1 andrewt andrewt 20000 Mar 24 10:39 lots_of_dots../file_sizes.sh Small files: a empty Medium-sized files:Large files: bbb e lots_of_dots |
Hint: you can use the command wc to discover how many lines are in a file. You probably want to use the shells back quotes, its if statement, and the test command.
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest file_sizes
Autotest Results
98% of 527 students who have autotested file_sizes.sh so far, passed the autotest test.
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab02_file_sizes file_sizes.sh
before Tuesday 16 June 17 59 to obtain the marks for this lab exercise.
Write a shell script scraping_courses.sh which prints a list of UNSW courses with the given prefix by extracting them from the 2018 UNSW handbook webpages.
This year UNSW has changed to much prettier format but also a format which it is much harder for a script to extract information from.
So for this exercise well use the 2018 handbook pages which arent For example:
scraping_courses.sh OPTMOPTM2111 Optometry 2AOPTM2133 The Clinical EnvironmentOPTM2190 Introduction to Clinical OptometryOPTM2211 Optometry 2BOPTM2233 Optical DispensingOPTM2291 Primary Care OptometryOPTM3105 Disease Processes of the Eye 1OPTM3111 Optometry 3AOPTM3131 Ocular Disease 3AOPTM3133 Vision Science in the Consulting RoomOPTM3201 Ocular Imaging & Applied Vision Science OPTM3205 Disease Processes of the Eye 2OPTM3211 Optometry 3BOPTM3231 Ocular Disease 3BOPTM3233 Working in the clinical environmentOPTM4110 Optometry 4AOPTM4131 Clinical Optometry 4AOPTM4151 Ocular Therapeutics 4AOPTM4211 Optometry 4BOPTM4231 Clinical Optometry 4BOPTM4251 Ocular Therapeutics 4BOPTM4271 Professional OptometryOPTM4291 Optometry, Medicine & Patient ManagementOPTM5111 Clinical Optometry 5AOPTM5131 Specialist Clinical Optometry 5AOPTM5151 Clinical Ocular Therapeutics 5AOPTM5171 Research Project 5AOPTM5211 Clinical Optometry 5BOPTM5231 Specialist Clinical Optometry 5BOPTM5251 Clinical Ocular Therapeutics 5B OPTM5271 Research Project 5BOPTM6400 Optometric Preclinical PracticeOPTM6411 Contact LensesOPTM6412 Clinical Optometry 4AOPTM6413 Anterior Eye TherapeuticsOPTM6421 Binocular Vision, Paediatrics and Low VisionOPTM6422 Clinical Optometry 4BOPTM6423 Therapeutics and the Posterior EyeOPTM6424 Professional OptometryOPTM7001 Introduction to Community Eye HealthOPTM7002 Epidemiology & Biostatistics for Needs AssessmentOPTM7003 Epidemiology of Blinding Eye DiseasesOPTM7004 Advocacy and Education in Community Eye Health OPTM7005 Eye Health Economics and SustainabilityOPTM7006 Eye Care Program ManagementOPTM7007 Community Eye Health ProjectOPTM7103 Behavioural Optometry 1OPTM7104 Advanced Contact Lens Studies 1OPTM7108 Research Skills in OptometryOPTM7110 Public Health OptometryOPTM7115 Visual NeuroscienceOPTM7117 Ocular Therapy 2OPTM7203 Behavioural Optometry 2 OPTM7205 Specialty Contact Lens StudiesOPTM7213 Ocular TherapyOPTM7301 Advanced Clinical OptometryOPTM7302 Evidence Based OptometryOPTM7308 Research ProjectOPTM7444 Business Skills in OptometryOPTM7511 Advanced Ocular Disease 1OPTM7521 Advanced Ocular Disease 2OPTM8511 Clinical paediatrics, low vision and colour visionOPTM8512 Clinical Optometry 5AOPTM8513 Clinical Ocular Therapy 5AOPTM8518 Optometry Research Project AOPTM8521 Clinical Contact LensesOPTM8522 Clinical Optometry 5BOPTM8523 Clinical Ocular Therapy 5B OPTM8528 Optometry Research Project B scraping_courses.sh MATH|wc |
126 585 4874scraping_courses.sh COMP|grep SoftCOMP1531 Software Engineering FundamentalsCOMP2041 Software Construction: Techniques and ToolsCOMP3141 Software System Design and ImplementationCOMP3431 Robotic Software ArchitectureCOMP4161 Advanced Topics in Software VerificationCOMP4181 Language-based Software SafetyCOMP6447 System and Software Security AssessmentCOMP9041 Software Construction: Techniques and ToolsCOMP9181 Language-based Software Safety COMP9322 Software Service Design and EngineeringCOMP9323 Software as a Service Project COMP9431 Robotic Software Architecture scraping_courses.sh MINE|grep RockMINE3630 Rock BreakageMINE8640 Geotechnical Hazards in Hard Rock MinesMINE8660 Geotechnical Engineering for Underground Hard Rock |
Your script must download the handbook web pages and extract the information from them when it is run.
Hints
This task can be done using the usual tools of egrep, sed, sort & uniq but the regular expressions take some thought.
The UNSW handbook uses seperate web pages for undergraduate and postgraduate courses. These two web pages would need to be downloaded for the above example (OPTM):
http://legacy.handbook.unsw.edu.au/vbook2018/brCoursesByAtoZ.jsp?StudyLevel=Undergraduate&descr=O and http://legacy.handbook.unsw.edu.au/vbook2018/brCoursesByAtoZ.jsp?StudyLevel=Postgraduate&descr=O. Make sure courses which occur in both postgraduate & undergraduate handbooks arent repeated. cat -A can be useful to check for non-printing characters.
The command curl will download a URL and print it to standard output.
In a script it is best run as curl silent so it doesnt print extra information on standard error.
For example:
curl silent http://legacy.handbook.unsw.edu.au/vbook2018/brCoursesByAtoZ.jsp?StudyLevel=Undergraduate&descr=O|grep OPTM<TD class= align=left>OPTM2111</TD><TD class=><A href=http://www.handbook.unsw.edu.au/undergraduate/courses/2018/OPTM2111.html>Optometry 2A</A></TD><TD class=evenTableCell align=left>OPTM2133</TD><TD class=evenTableCell><Ahref=http://www.handbook.unsw.edu.au/undergraduate/courses/2018/OPTM2133.html>The Clinical Environment </A> </TD><TD class= align=left>OPTM2190</TD><TD class=><Ahref=http://www.handbook.unsw.edu.au/undergraduate/courses/2018/OPTM2190.html>Introduction to Clinical Optometry </A></TD><TD class=evenTableCell align=left>OPTM2211</TD><TD class=evenTableCell><A href=http://www.handbook.unsw.edu.au/undergraduate/courses/2018/OPTM2211.html>Optometry 2B</A></TD> <TD class= align=left>OPTM2233</TD><TD class=><A href=http://www.handbook.unsw.edu.au/undergraduate/courses/2018/OPTM2233.html>Optical Dispensing </A></TD> |
The program wget can be used for the same purpose, by running it as wget -q -O- url
When you think your program is working, you can use autotest to run some simple automated tests:
$ 2041 autotest scraping_courses
Autotest Results
82% of 190 students who have autotested scraping_courses.sh so far, passed all autotest tests. 91% passed test 1
84% passed test 2
87% passed test 3
88% passed test 4
86% passed test 5
When you are finished working on this exercise, you must submit your work by running give:
$ give cs2041 lab02_scraping_courses scraping_courses.sh
before Tuesday 16 June 17 59 to obtain the marks for this lab exercise.
When you are finished each exercises make sure you submit your work by running give.
You can run give multiple times. Only your last submission will be marked.
Dont submit any exercises you havent attempted.
If you are working at home, you may find it more convenient to upload your work via gives web interface.
Remember you have until Tuesday 16 June 17 59 59 to submit your work.
You cannot obtain marks by e-mailing your code to tutors or lecturers.
You check the files you have submitted here.
Automarking will be run by the lecturer several days after the submission deadline, using test cases different to those autotest runs for you. (Hint: do your own testing as well as running autotest.)
After automarking is run by the lecturer you can view your results here. The resulting mark will also be available via gives web interface.
Reviews
There are no reviews yet.