KIT104 ICT Architecture and Operating Systems Semester 2, 2016
Assignment Two – UNIX Shell Programming
(15% of your KIT104 final result)
Due at 3pm Thursday Week 12, 6 October 2016
This is an individual assignment. There are three (3) shell programming tasks in this assignment.
You are required to make a directory named kit104agn2 under your home directory (on alacritas), and use kit104agn2 as your working directory for this assignment. Your assignment will be marked on alacritas, one of our School’s UNIX servers.
TaskA
Write a shell script (to run on the Bourne shell) that can be used to remove some old C programs you no longer wish to keep. When the script is run with no arguments supplied, it picks up each C program from the current directory and lists the first 10 lines (hint: research the head command). It then prompts for deletion of the file (the user can then choose to delete or not to delete the file). When the user supplies arguments (C program file names) with the script, it works on those files only.
Your script for this task must be named delC.sh. In designing your script you should consider the following scenarios:
There is no C program file stored under the current directory;
There is at least one C program file stored under the current directory;
The user-supplied arguments list contains names of existing C
programs stored under the current directory. We can assume that all the C programs have been correctly named as *.c, and that there is no special character (such as space) in any of these filenames;
The user-supplied arguments list contains both existing and missing names of C programs stored under the current directory. Missing names refer to the files which no longer exist under the current directory.
Make sure your script is user-friendly and follows common sense (for example, when there is no C program stored under the current directory your
KIT104 Assignment 2, 2016
Page 1 of 10
script should display a message and then exit). The following is a sample output of the script. The $ is the shell prompt.
$ ./delC.sh
This script removes C files which you no
Here are the C file(s) under the current
No C files found.
$ ./delC.sh
This script removes C files which you no
Here are the C file(s) under the current
f1.c f2.c
Displaying first 10 lines of f1.c:
/* A C program for handling arrays
Written by John Jones, July 2009
Updated April 2010
*/
#include
int main() {
int x;
Delete file f1.c? (y/n):n
File f1.c NOT deleted.
Displaying first 10 lines of f2.c:
/* Another C program for handling arrays
Written by John Jones, August 2009
Updated May 2010
*/
#include
int main()
{
int x, y, z;
Delete file f2.c? (y/n): y
File f2.c deleted.
$ ./delC.sh f1.c
This script removes C files which you no
The file(s) you want to delete is/are:
f1.c
Displaying first 10 lines of f1.c:
/* A C program for handling arrays
Written by John Jones, July 2009
Updated April 2010
*/
#include
int main()
longer want to keep.
directory:
longer want to keep.
directory:
longer want to keep.
KIT104 Assignment 2, 2016
Page 2 of 10
(Continued next page)
KIT104 Assignment 2, 2016
{
int x;
Delete file f1.c? (y/n):y
File f1.c deleted.
$ ./delC.sh f2.c f3.c
This script removes C files which you no longer want to keep. The file(s) you want to delete is/are:
f2.c f3.c
Displaying first 10 lines of f2.c:
/* Another C program for handling arrays
Written by John Jones, August 2009
Updated May 2010
*/
#include
int main()
{
int x, y, z;
Delete file f2.c? (y/n): y
File f2.c deleted.
Displaying first 10 lines of f3.c:
File f3.c does not exist.
$ ./delC.sh f3.c f4.c
This script removes C files which you no longer want to keep. The file(s) you want to delete is/are:
f3.c f4.c
Displaying first 10 lines of f3.c:
File f3.c does not exist.
Displaying first 10 lines of f4.c:
File f4.c does not exist.
$
Page 3 of 10
Task B
Write a shell script (to run on the Bourne shell) that runs an infinite loop to monitor the creation and removal of .pdf or .PDF files under the current directory. Every 3 seconds it should display a list of those filenames created or removed after the previous display. Without loss of practical significance of this utility, we can assume that the time interval between creation of a .PDF or .pdf file and removal of a .PDF or .pdf file is over 3 seconds, which means that it’s unnecessary for your script to handle the situation where creation of a file is followed by immediate removal of a file. (Hint: research the cmp and comm commands.)
Your script for this task must be named pdf.sh. The following is a sample output of the script (It is OK that the script leaves behind a temporary file when it is finally interrupted). The $ is the shell prompt.
KIT104 Assignment 2, 2016
$ ./pdf.sh
No pdf files have been created or removed in the last 3 seconds. No pdf files have been created or removed in the last 3 seconds.
The following pdf file(s) have been created in the last 3 seconds: a3.pdf
a4.pdf
a5.pdf
The following pdf file(s) have been removed in the last 3 seconds: a4.pdf
a5.pdf
No pdf files have been created or removed in the last 3 seconds. No pdf files have been created or removed in the last 3 seconds. ……………
(Continued next page)
Page 4 of 10
Task C
Write a shell script (to run on the Bourne shell) that allows a user to view or delete a setting in a configuration file (config.txt) that contains settings in the form variable=value. The following is an example of such configuration file:
Your script for this task must be named setting.sh. For ease of use, your script must present a menu of operations that a user may choose from. After the user makes a selection and that the selected operation has been completed, the menu must be displayed again so that the user can make another selection. Validation check on user inputs is required (see the following sample output about this). In the beginning of your script you need to check to see whether the required configuration file (config.txt) actually exists under the current directory (if not, your script displays a message and then exits).
Here is a sample output of your script. The $ is the shell prompt. The items in italics are not part of the sample output. They are hints indicating how your script should behave.
KIT104 Assignment 2, 2016
HOME=/u/soc/abc
HOST=lawson
HOSTTYPE=sun4
LOGNAME=abc
OSTYPE=solaris PATH=/usr/dt/bin:/usr/openwin/bin:/bin:. PS1=$
PS2=>
SHELL=/usr/bin/tcsh
TZ=Australia/Tasmania
USER=abc
VENDOR=sun
EDITOR=joe
$ ./setting.sh
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – Quit
CHOICE: 1 (user input)
Enter variable name: EDTOR (user input)
Variable does not exist (Your script needs to check whether a variable exists or not)
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Page 5 of 10
Q – Quit
CHOICE: 1 (user input)
Enter variable name: EDITOR (user input)
EDITOR=joe
Delete this setting (y/n)? y (user input)
Setting deleted (However, if user’s answer is n here, then the setting stays)
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – Quit
CHOICE: 2
Enter variable name: USER1 (user input)
Variable does not exist (Your script needs to check whether a variable exists or not)
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – Quit
CHOICE: 2 (user input)
Enter variable name: USER (user input) USER=abc
Requested setting displayed above
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – Quit
CHOICE: 3 (user input)
HOME=/u/soc/abc
HOST=lawson
HOSTTYPE=sun4
LOGNAME=abc
OSTYPE=solaris PATH=/usr/dt/bin:/usr/openwin/bin:/bin:. PS1=$
PS2=>
SHELL=/usr/bin/tcsh
TZ=Australia/Tasmania
USER=abc
VENDOR=sun
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – Quit
KIT104 Assignment 2, 2016
Page 6 of 10
KIT104 Assignment 2, 2016
CHOICE: 4 (user input) Invalid choice
*** MENU ***
1. Delete a Setting
2. View a Setting
3. View All Settings
Q – quit
CHOICE: q (user input)
(The running script is terminated. The shell prompt is displayed)
Page 7 of 10
For All Your Scripts
You must
• Include your name, student ID, and a brief introduction of what the
script does in all your shell scripts, as a comment in the beginning of
each script.
• Make your scripts run on the Bourne shell, regardless of which shell
the user of your scripts is currently on.
• Add in-line comments to help other people understand your scripts.
• Use “\n” where appropriate to make the output of your scripts more
readable.
• Note that your script structure and layout are also important as they
will be marked as part of the assessment process.
Submitting Your Assignment
You must submit your kit104agn2 folder containing the following four (4) files electronically:
delC.sh, pdf.sh, setting.sh, config.txt
(Please make sure that your kit104agn2 folder only contains the four files as
listed here, when you are ready to submit them)
You can use a Windows PC (in one of our school’s Windows PC labs) to submit your assignment. This can be done by simply selecting your assignment folder and copy it: In Windows, right-click on your kit104agn2 folder and choose copy from the pop-up context menu. Navigate to the kit104submit folder (which is located in your alacritas account [M: drive in the school computer lab]), right-click on it, and choose paste from the pop-up menu. You should look in the kit104submit folder to verify that your assignment files are there.
Note that the kit104submit folder will be created for you automatically close to the submission time – do not create it yourself as this may cause your assignment to not be submitted correctly. If the kit104submit folder does not exist, please visit the School Help Desk.
You must also submit a signed cover sheet to the KIT104 assignment box located at the School Help Desk or Reception. Assignments without a signed coversheet will NOT be marked.
Please note: It is your responsibility to ensure that your assignment has been successfully submitted to the correct folder on alacritas. If you require assistance with this please visit the School Help Desk.
KIT104 Assignment 2, 2016
Page 8 of 10
If your assignment is late then you should submit your files to the kit104late folder. The late folder will be created automatically after the due date and will be available for one week only.
Assignment submissions will be marked by the KIT104 tutors on alacritas. If you wrote and tested your scripts elsewhere it’s your responsibility to ensure that your scripts run correctly on the School UNIX system.
Need Help?
You are encouraged to seek assistance from your lecturer after you have seriously thought about the assignment. Please note that we can provide general advice, however, we will not help you write any code, nor will we help you debug.
(See next page for the assignment marking scheme)
KIT104 Assignment 2, 2016
Page 9 of 10
Appendix: KIT104 Assignment 2 Marking Scheme
Script delC.sh (for each item there are only 3 possible marks: 100% or 50% or 0%)
Script pdf.sh (for each item there are only 3 possible marks: 100% or 50% or 0%)
Script setting.sh (for each item there are only 3 possible marks: 100% or 50% or 0%)
Others (for each item there are only 3 possible marks: 100% or 50% or 0%)
Assignment 2 Total: /40
KIT104 Assignment 2, 2016
Mark
Out of
Execution of the shell script
Script runs as expected (2). Script runs but not as expected (1). Script does not run (0)
2
Display a message then exit when there is no C file under current directory
2
Pick up each C file from current directory
2
List the first 10 lines then prompt for deletion
2
When user supplies C file names as arguments work on those files only
2
Can handle invalid file names contained in user-supplied arguments list
2
Mark
Out of
Execution of the shell script
Script runs as expected (2). Script runs but not as expected (1). Script does not run (0)
2
Correctly set up infinite loop
2
Display every 3 seconds “No pdf files have been created or removed … …” when this is true
2
Correctly display names of the files that have been created
2
Correctly display names of the files that have been removed
2
Mark
Out of
Execution of the shell script
Script runs as expected (2). Script runs but not as expected (1). Script does not run (0)
2
Correctly allow user to remove settings
2
Correctly allow user to view a setting
2
Correctly allow user to view all settings
2
Checking the existence of the configuration file
2
Checking the existence of the variable name for deleting or viewing a setting
2
Mark
Out of
Shell scripts structure and layout
Clear and tidy (2). Somewhat messy but understandable (1). Messy (0)
2
Appropriate comments
2
Include name, ID, and brief introduction in all scripts
2
Page 10 of 10