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%.
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.
$ . ./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!
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.
We assume:
The user gives options (if any) before pathnames (if any).
If the user intends a pathname to be -a
for example,
they will definitely insert --
to disambiguate, e.g., this
means append -a
:
editpath -a -- -a
If you use getopts
, you get this feature for
free!
Pathnames are non-empty. Pathnames do not contain newlines, backslashes, single quotes, double quotes, colons. However, almost any other characters can be fair game, including spaces, $, #.
The original value of PATH does not contain newlines, backslashes, single quotes, double quotes. However, almost any other characters can be fair game, including spaces, $, #.
Every colon in the original value of PATH sits between two non-empty pathnames.
Don’t check that user-provided pathnames actually exist as directories, or is/isn’t already present in PATH. Just obey the user.
E.g., if the user adds the same pathname multiple times, do it.
E.g., if the user deletes a pathname that does not occur in PATH, this is not an error, the new PATH is identical to the old PATH.
Please define your editpath
function in def-editpath.sh
and hand it in. You may add helper functions.