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.24T2.tutors Alias: cs2041.24T1.tutors Description: 24T1 COMP2041 tutors Flags: personal, public, unprivileged, members_can_post, closed Addresses: z5364339 z5362057 z5358476 z5416950 z5115658 z5492331 z5312070 z5360130 z5310685 z5358496 z3121449 z5309949 z5363653 z5421623 z5478419 z5311762 z5361515 z5420849 z5419020 z5437869 z5361210 z5359557 z5422337 z5425577 z5420531 z5359448 z5366832 z5363122 z5421214 z5418202 z5358896 z5208931 z5483411 andrewt Owners: cs2041 Senders: @COMP2041_Lecturer, @COMP2041_Supervisor, @Employee
Convert the output of the
mlalias
command into a new line separated list of UNSW zIDs,
like this:z5364339 z5362057 z5358476 z5416950 z5115658 z5492331 z5312070 z5360130 z5310685 z5358496 z3121449 z5309949 z5363653 z5421623 z5478419 z5311762 z5361515 z5420849 z5419020 z5437869 z5361210 z5359557 z5422337 z5425577 z5420531 z5359448 z5366832 z5363122 z5421214 z5418202 z5358896 z5208931 z5483411
-
CSE system have a command,
acc
, which prints information about a specified user.
For example:acc z5417087 User Name : z5417087 Aliases : xavc Uid : 3731 Gid : 3731 Expires : 16mar2025 Groups : cs1521[23sep2024], classadmin[16jun2025] Group classes : classadmin[16jun2025] User classes : 3779_Student, COMP1521_Supervisor[23sep2024] : COMP4961t2_Student[06oct2024] : COMP6453t2_Student[23sep2024] Misc classes : Name : Mr Cooney, Xavier (Xavier Cooney) Position : Casual Academic (Sch: Computer Science & Eng) UNSW Number : 5417087 UNSW Mail : x.cooney@unsw.edu.au UNSW Home : //INFPWFS219.ad.unsw.edu.au/Student037$/z5417087 CSE Home : /import/reed/5/z5417087
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:COMP4961 COMP6453
Make sure you don't include COMP1521, as that is a supervisor role (course admin).
-
Use the pipeines from the above 2 questions to write a shell script which print 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 the files 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 system linux.
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, uses to ls 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.