CSCB09 2023 Summer Assignment 1

Due: June 7 Wednesday 11:59PM
This assignment is worth 10% of the course grade.

In this assignment, you will implement an interesting shell script and learn/use some Unix utility programs.

As usual, you should aim for reasonably efficient algorithms and reasonably organized, comprehensible code.

Correctness (mostly auto-testing) is worth 90% of the marks; code quality is worth 10%.

Editing PATH (10 marks)

We understand that the environment variable PATH is very annoying to edit! While appending and prepending are not too bad (but still not great), deleting something is really horrendous.

Let’s write a shell function to help with that! (Exercise: Why function?)

Write a shell function editpath that takes 1 or more options followed by 0 or more arguments that we consider to be pathnames. The user may or may not insert -- between the last option and the first pathname. So the syntax in Unix parlance is:

editpath OPTION... [--] [PATHNAME]...
option meaning
-a append the pathnames (1.5 marks)
-p prepend the pathnames (1.5 marks)
-d delete the pathnames (6 marks)
none illegal (1 mark)

If 2 or more options are given, the last one takes priority. If 0 options are given, the function returns 1 without changing PATH; error messages are optional but please send to stderr only.

Please define your editpath function in def-editpath.sh and hand it in. You may add helper functions.

Examples

$ . ./def-editpath.sh
$ PATH=/bin:/usr/bin:/usr/local/bin
$ editpath -a '/xxx   yyy' /opt/bin .
$ /usr/bin/printenv PATH
/bin:/usr/bin:/usr/local/bin:/xxx   yyy:/opt/bin:.
$ editpath -p 'Job$' '/M$Office' x
$ /usr/bin/printenv PATH
x:/M$Office:Job$:/bin:/usr/bin:/usr/local/bin:/xxx   yyy:/opt/bin:.
$ editpath -d . 'Job$' usr
$ /usr/bin/printenv PATH
x:/M$Office:/bin:/usr/bin:/usr/local/bin:/xxx   yyy:/opt/bin

The provided script example.sh contains the commands above. Test it with sh example.sh. The provided file expected.txt is the exact expected output.

You do know how to use pipelining/redirection and the diff program to check your output against expected.txt, right? Automarking will not accept any “invisible” difference in blanks and unprinted bytes. B09 is one of the few courses where you develop this sensitivity, as all programmers are supposed to!

“You will also need”

The grep program can help you delete; find out which of its non-default options make your life simplest. But note that other familar utility programs are needed too.

This assignment is designed to be easily solved without creating intermediate “temp” files. Automarking will run tests in a docker container with a read-only file system.

Assumptions

We assume:

Handing In

Please define your editpath function in def-editpath.sh and hand it in. You may add helper functions.