Week 04 Tutorial Questions
Objectives
-
A COMP2041 student wrote this script, named start_lab04.sh, to run before the Week 4 lab.
cat start_lab04.sh #! /bin/dash cd ~/labs/04 ex1=jpg2png ex2=email_image ex3=date_image ex4=tag_music
But when he ran his script, it didn't seem to work:
pwd /home/z1234567 ./start_lab04.sh pwd /home/z1234567 echo $ex1 $ex2 $ex3 $ex4
Why not? How can the script be fixed?
-
Write a shell script,
is_business_hours
which exits with a status of 0 if the current time is between 9am - 5pm, and otherwise exits with a status of 1. -
COMP2041 student Shruti has a 'friends' subdirectory in her home directory that contains images of her many friends.
Shruti likes to view these images often and would like to have them appear in other directories within her CSE account so she has written a shell script to symlink them to the current directory:for image_file in $(ls ~/friends); do ln -s "~/friends/$image_file" . done
The links created by Shruti's script are broken.
Why? How can she fix her script?
-
The course code for COMP2041 has been changed to COMP2042 and the course code for COMP9044 has been changed to COMP9042.
Write a shell script,
update_course_code.sh
which appropriately changes the course code in all the files it is given as arguments. -
Modify
update_course_code.sh
so if given a directory as an argument it updates the course codes in files found in that directory and its sub-directories. -
CSE systems have a command,
mlalias
, which prints information about a specified mail alias.
For example:mlalias cs2041.25T2.tutors Alias: cs2041.25T2.tutors Description: Maling List For COMP(2041|9044) 2025 T2 Tutors Flags: personal, public, unprivileged, members_can_post, closed Addresses: andrewt z9300162 dylanb z5115658 annab z5218654 z3121449 z5075632 z5160350 z5208931 z5256103 z5310685 z5311762 z5316004 z5358496 z5358896 z5360341 z5360529 z5361210 z5361534 z5362371 z5367318 z5416950 z5419252 z5420326 z5420849 z5421214 z5425577 z5428854 z5431362 z5437869 z5477887 z5477935 z5478419 z5479211 z5481971 z5483411 z5487536 z5487891 z5488112 z5488548 z5492103 z5513422 z5591556 z5592103 z5612340 Owners: cs2041 Senders: @COMP2041_Lecturer, @COMP2041_Supervisor
Convert the output of the
mlalias
command into a new line separated list of UNSW zIDs,
like this:z9300162 z5115658 z5218654 z3121449 z5075632 z5160350 z5208931 z5256103 z5310685 z5311762 z5316004 z5358496 z5358896 z5360341 z5360529 z5361210 z5361534 z5362371 z5367318 z5416950 z5419252 z5420326 z5420849 z5421214 z5425577 z5428854 z5431362 z5437869 z5477887 z5477935 z5478419 z5479211 z5481971 z5483411 z5487536 z5487891 z5488112 z5488548 z5492103 z5513422 z5591556 z5592103 z5612340
-
CSE systems have a command,
acc
, which prints information about a specified user.
For example:acc z5218654 User Name : z5218654 Aliases : annab Uid : 41545 Gid : 41545 Expires : 15mar2026 Groups : cs2041[29sep2025], cs1521[29sep2025] Group classes : User classes : 3785_Student, COMP1521_Supervisor[29sep2025] : COMP2041_Supervisor[29sep2025] : COMP3311t2_Student[29sep2025] Misc classes : Name : Ms Brew, Anna (Anna Brew) Position : Casual Academic (Sch: Computer Science & Eng) UNSW Number : 5218654 UNSW Mail : a.brew@unsw.edu.au UNSW Home : //INFPWFS219.ad.unsw.edu.au/Student039$/z5218654 CSE Home : /import/reed/4/z5218654
Write a pipeline which converts the output of acc into a new line separated list of courses the person is enrolled as a student in,
like this:COMP3311
Make sure you don't include COMP1521 or COMP2041, as these are a supervisor role (course admin).
-
Use the pipelines from the above 2 questions to write a shell script which prints a list of courses taken by COMP2041 tutors with counts of how many COMP2041 tutors take each,
like this:7 COMP6771 6 COMP3900 6 COMP3331 5 COMP3311 5 COMP3121 ...
-
Write a shell script named
is_prime.sh
which given an integer as an argument, tests whether it is prime and prints a suitable message:is_prime.sh 42 42 is not prime is_prime.sh 113 113 is prime
Your script should exit with a non-zero exit status if its argument is not prime.
Write a second script named
primes.sh
which uses the first script to print all primes less than a specified value, e.g:primes.sh 100 2 3 5 7 11 13 17 ... 79 83 89 97
-
We are working on a C program in an editor, and we'd like to run a script in another window which recompiles the program every time we save the file in the editor.
Write a shell script named
recompile.sh
which given a C file as argument, recompiles the program if the file changes and if the compile succeeds runs the program.Hint: you can use the program
stat
to print the time when a file was last modified (as seconds since 1970-01-01) , like this:$ stat -c '%Y' main.c 1615346166
Repeatedly checking if a file has changed is not ideal. It consumes CPU/power.
The program
inotifywait
is available on many Linux systems.
inotifywait -e main.c
will exit whenmain.c
is modifiedinotifywait
is efficient because waits for the operating system interface to notify it the file has changed..Modify your script to use
inotifywait
-
The shell variable
$PATH
contains a colon-separated list of directories. which will be searched when executing a command.Write a shell script named
which.sh
which given a program name as argument, usesls
to print details of matching files in directories in$PATH
.For example:
./which.sh cat -rwxr-xr-x 1 root root 43936 Sep 24 18:36 /bin/cat ./which.sh clang llrwxrwxrwx 1 root root 24 Jan 12 01:49 /usr/bin/clang -> ../lib/llvm-11/bin/clang ./which.sh lost lost not found
The shell builtin
which
does something similar:which cat /bin/cat which clang /usr/bin/clang which lost
but don't try using
which
. Use the usual programs we've been using to write scripts such as tr and test.Think about if any characters in directory names migh break your answer, e.g spaces.