KIT501 UNIX Scripting Assignment
Due Date: 3:00pm Friday the 9th of October – Week 12 Introduction
There are two (2) shell programming tasks in this assignment. This is an individual assignment. You are required to make a directory named kit501a2 under your home directory (on ictteach) and use kit501a2 as your working directory for this assignment.
Assignment submissions will be marked by the KIT501 tutors on ictteach (also known as alacritas). If you write and test your scripts elsewhere it’s your responsibility to ensure that your scripts run correctly on the School UNIX system (ictteach). You must also submit your assignment via this server (see later).
Please pay close attention to the marking scheme shown on the last page. This scheme shows you how marks are allocated, and what you must (successfully) achieve to receive marks. Note also that your scripts must have clear and tidy structure, have good use of indentation and whitespace (to make the script readable), include appropriate comments, and have your identification details and the script’s purpose specified in comments at the top of your script.
Task A
Write a shell script (to run on the Bourne shell) called cp2.sh to copy all the files from two named directories into a new third directory. Time stamps must be preserved while copying files from one directory into another.
For example:
cp2.sh dir1 dir2 dir3
This example will copy all of the files in dir1 and dir2 to dir3.
Continued on next page…
Ø Note: dir1, dir2 and dir3 are example directory names. Your script should not check for these example values explicitly. Instead, your script should allow any directory names to be entered by the user, and then it uses those values entered for any further commands that refer to the directories.
1
KIT501 ICT Systems Administration Fundamentals Unix Scripting Assignment
Task Requirements
1. Three directory names must be supplied as arguments to your script when it is executed by the user.
2. In the beginning of your script you need to check that the first two directory names provided (e.g. dir1 and dir2) exist under the current directory. If they do not exist, your script must display an error message and then it should exit.
3. The third directory argument to the script will specify a new directory name (for example, dir3). This directory must not currently exist (i.e. the creation of e.g. dir3 should be one of the actions of your script). If the directory already exists, your script should display an error message and exit.
4. The script will first copy all of the files from dir1 into dir3. Your script output must indicate which files have been copied.
5. The script will then copy every file from dir2 to dir3 subject to the following conditions:
a. only copy if the file from dir2 is not already present in dir3; or
b. only copy if the file from dir2 is newer than the same file in dir3. The newer file
will overwrite the existing version in dir3.
Your script output must indicate which files have been copied.
6. Clean up – remove all temporary files created (if any) at the end of your script.
7. Your script must generate output similar to the example output shown below.
Example output
The following is a sample output of the script. The $ is the shell prompt.
Ø HINT: You should note that the cp command without any option does not preserve certain attributes (such as ownership and timestamps)
$ ./cp2.sh dir1 dir2 dir3
These files from dir1 copied into dir3: a.c b.c file1.c
These new file(s) from dir2 copied into dir3: file2.c
These file(s) from dir2 copied into dir3 and overwrite(s) existing older versions in dir3:
a.c
2
KIT501 ICT Systems Administration Fundamentals Unix Scripting Assignment
The following sample outputs verify the result shown previously so you can see the example file names and timestamps used.
$ ls
cp2.sh dir1 dir2 dir3
$ ls -l dir1
total 12
-rw-r–r– 1
-rw-r–r– 1
-rw-r–r– 1
$ ls -l dir2
total 12
-rw-r–r– 1
-rw-r–r– 1
-rw-r–r– 1
$ ls -l dir3
total 16
-rw-r–r– 1
-rw-r–r– 1
-rw-r–r– 1
-rw-r–r– 1
sxu staff 9 Aug 2 12:52 a.c sxu staff 9 Aug 2 12:52 b.c sxu staff 9 Aug 2 12:52 file1.c
sxu staff 9 Aug 2 12:53 a.c sxu staff 9 Aug 2 12:51 b.c sxu staff 9 Aug 2 12:53 file2.c
sxu staff 9 Aug 2 12:53 a.c sxu staff 9 Aug 2 12:52 b.c sxu staff 9 Aug 2 12:52 file1.c sxu staff 9 Aug 2 12:53 file2.c
Task B
Dominion Consulting in Sydney needs a shell script (to run on the Bourne shell) to maintain its employee records file, which contains the following information (fields) about each employee:
• telephone number (8 digits, first digit non-zero);
• family name (alphabetic characters);
• first name (alphabetic characters);
• department number (2 digits) and
• job title (alphabetic characters).
This script should let users add, delete, search for, and display specific employee information.
Continued on next page…
3
KIT501 ICT Systems Administration Fundamentals Unix Scripting Assignment
Task Requirements
1. Create a text file named records containing the following records with fields delimited by colons (:) :
95671660:Jones:Sarah:45:sales manager 93272658:Smith:John:43:technical manager 98781987:Williams:Nick:35:computer officer 99893878:Brown:Sarah:12:electrician 95673456:Couch:David:26:chef 95437869:Anderson:Sarah:19:CEO
2. In the beginning of your script you need to check to see whether the required text file (records) actually exists under the current directory. If records does not exist, your script must display an error message and then exit.
3. The script must be named menu.sh. The script must show a menu of operations (see requirement 4) that a user may choose from. Among other tasks, these operations automate the following four activities:
1) Display all current employee records to the screen;
2) Search for and display specific employee record(s) (search all fields, ignoring case);
3) Add new records to the records file; and
4) Delete records from the records file.
4. The script must show the following menu and prompt the user to select an option:
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
5. After a user chooses an option and the selected operation has been completed, the user is prompted to press the Enter key, and then the menu must be displayed again so that the user can make another selection.
Continued on next page…
4
KIT501 ICT Systems Administration Fundamentals Unix Scripting Assignment
6. When adding a new employee record (option 3), the script must ask the user to provide the following field values (in order):
phone number family name
given name department number job title
Each field must be correctly validated before the next field is requested. If validation fails, the user must be asked to enter the field again (and this process repeats until the field is successfully validated). After each successful employee record is added, the user should be prompted to ask if they want to enter another employee record (this process will continue until the user indicates they do not want to add another employee record).
Validation is as follows:
• family name:
• given name:
• job title:
• phone number:
must be alphabetic characters and/or spaces.
must be alphabetic characters and/or spaces.
must be alphabetic characters and/or spaces.
the first digit of an eight-digit phone number must not be zero. Each telephone number must be unique (not already exist).
• department number: A valid department number must only contain 2 digits.
7. When deleting records (option 4), the user must be prompted to enter a valid phone number. If the phone number is invalid, then the user will continue to be prompted until they enter a valid phone number. If a record matches the phone number entered (hint: as phone numbers are unique, only one record should match), then the matching record must be displayed, and the user must be prompted to confirm they want to delete the matching record.
Example output
Here is a sample output of the script which will indicate the main semantics of each option. 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. Items in green are examples of user input.
$ ./menu.sh
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
5
KIT501 ICT Systems Administration Fundamentals
Unix Scripting Assignment
Your Selection: 1(user input) 95671660:Jones:Sarah:45:sales manager 93272658:Smith:John:43:technical manager 98781987:Williams:Nick:35:computer officer 99893878:Brown:Sarah:12:electrician 95673456:Couch:David:26:chef 95437869:Anderson:Sarah:19:CEO
Press Enter to continue… (user presses Enter here)
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 5(user input) Invalid selection
Press Enter to continue… (user presses Enter here)
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 2(user input) Enter keyword: Sarah (user input)
95671660:Jones:Sarah:45:sales manager 99893878:Brown:Sarah:12:electrician 95437869:Anderson:Sarah:19:CEO
Press Enter to continue… (user presses Enter here)
6
KIT501 ICT Systems Administration Fundamentals
Unix Scripting Assignment
Continued on next page…
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 2 (user input)
Enter keyword: Monks (user input)
Monks not found
Press Enter to continue… (user presses Enter here)
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 2(user input)
Enter keyword: (user simply presses Enter without typing in anything)
Keyword not entered
Press Enter to continue… (user presses Enter here)
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 3(user input) Add New Employee Record
Phone Number (xxxxxxxx): Phone number not entered Phone Number (xxxxxxxx): Invalid phone number Phone Number (xxxxxxxx): Phone number exists Phone Number (xxxxxxxx):
(user simply presses Enter without typing in anything) 00123456 (user input)
95437869 (user input)
99887766 (user input)
Continued on next page…
7
KIT501 ICT Systems Administration Fundamentals
Unix Scripting Assignment
Family Name: abc123 (user input)
Family name can contain only alphabetic characters and spaces Family Name: Warren (user input)
Given Name: Tony5 (user input)
Given name can contain only alphabetic characters and spaces Given Name: Tony (user input)
Department Number: 123 (user input)
A valid department number contains 2 digits Department Number: 23 (user input)
Job Title: accountant1 (user input)
Job title can contain only alphabetic characters and spaces Job Title: accountant (user input)
Adding new employee record to the records file …
New record saved (display this only after saving record is successful)
Add another? (y)es or (n)o: n (user input – note: allow for y,Y, n,N)
(Note: if the user answer is y or Y here then the above procedure is repeated to allow another employee record to
be added)
Dominion Consulting Employees Info Main Menu ============================================ 1 – Print All Current Records
2 – Search for Specific Record(s)
3 – Add New Records
4 – Delete Records
Q – Quit
Your Selection: 4(user input) Delete Employee Record
Enter a phone number: 05671660 (user input) Invalid phone number
Enter a phone number: 99999999 (user input) Phone number not found
Enter a phone number: 95671660 (user input) 95671660:Jones:Sarah:45:sales manager
Confirm deletion: (y)es or (n)o: y (user input – note: allow for y,Y, n,N)
Record deleted. (This message is displayed only after this record has been successfully deleted from the records file. (If the user answer is n then the main menu is displayed again. Then if the user enters Q or q then the script is terminated.)
8
KIT501 ICT Systems Administration Fundamentals Unix Scripting Assignment
General Requirements
You must:
• Include your full 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 using.
• Add in-line comments to help other people understand your scripts.
• Use newlines (“\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 (copy) a single folder named kit501a2 which contains the following three (3) files to the kit501submit folder which will be automatically created for you in your ictteach account:
cp2.sh, menu.sh, records
Please ensure your kit501a2 folder only contains the three files as listed here, when you are ready to submit your assignment.
Ø WARNING: Do not create the kit501submit folder. It will be created for you closer to the submission date. If you create this folder yourself, it will cause your assignment to not be submitted correctly1. In the week prior to the assignment due date, if the kit501submit folder does not exist, please advise the Unit Coordinator. Without a kit501submit folder, you will NOT be able to submit your assignment. If you see this folder in your home directory, DO NOT DELETE IT.
To submit your assignment, follow these two steps:
1. Ensure that both your kit501a2 folder and the kit501submit folder are present under
your home directory (see the warning above);
2. Type the following commands to copy your kit501a2 folder into your kit501submit
folder (the $ is the shell prompt):
1 Why? The kit501submit folder is actually a soft-link to another folder elsewhere in the filesystem. If you create the folder yourself, it will not be linked to the correct submission master folder, meaning your assignment will not be discovered or marked.
9
KIT501 ICT Systems Administration Fundamentals
Unix Scripting Assignment
$ cd
$ cp -r kit501a2 kit501submit
$ ls -l kit501submit
$ ls -l kit501submit/kit501a2
(This takes you back to your home directory from anywhere)
(This copies your assignment folder into your kit501submit folder. The option -r is important)
(This verifies that your assignment folder is now in your kit501submit folder)
(This verifies that your assignment files are now in your kit501submit folder)
If your assignment is late then you should submit your files to the kit501late folder. This late folder will be created immediately after the assignment due date and it will be available for one week only. Do not create this folder yourself, it will be created automatically.
Need Help?
You are encouraged to seek assistance from your lecturer or tutor after you have seriously thought about the assignment. Please note that we can provide general advice – but you are expected to write and debug (correct) your own code.
Ø Note: all of the knowledge you need for this assignment will have been discussed in the unit’s practical classes (up to and including week 10). Shell functions are discussed in week 11 but you are not required to use these in your assignment. You do not need to find additional syntax or commands from other sources (such as other websites discovered via search engines like google). You are also expected to write your script solutions using the particular methods of syntax as discussed in the unit.
Plagiarism
Plagiarism is a very serious matter, and ignorance of this is not an excuse. If you submit work claiming it to be your own, and it is not original work of your own, this is cheating. This means for example that you cannot submit code written by other students or sourced from the Internet and claim you wrote the code yourself. Plagiarism can result in academic penalties both in relation to your assignment, and also on your permanent university record.
Acknowledgements:
This assignment has been re-formatted and slightly modified from an assignment written by Dr Shuxiang Xu.
10
KIT501 ICT Systems Administration Fundamentals
Unix Scripting Assignment
KIT501 Assignment Marking Scheme
cp2.sh (for each item there are only 3 possible marks: 100% or 50% or 0%)
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
Check that dir1 and dir2 currently exist and that dir3 does not currently exist.
2
Copy all files from dir1 into dir3
2
Copy new files from dir2 into dir3
2
Copy newer files from dir2 to dir3 and overwrite older versions
2
menu.sh (for each item there are only 3 possible marks: 100% or 50% or 0%)
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 a loop to repeatedly display the menu
2
Correctly display all records
2
Correctly search for specific records
2
Correctly add new records
2
Validate each field of a new record correctly when adding new records
2
Correctly delete records
2
Common (for each item there are only 3 possible marks: 100% or 50% or 0%)
Mark
Out of
Shell scripts structure and layout
Clear and tidy (4).
Somewhat messy but understandable (2). Messy, confused, inconsistent (0)
2
Both scripts include name, ID, and brief introduction
2
Appropriate in-line comments
2
Assignment Total: /30
11