联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> C/C++编程C/C++编程

日期:2020-10-13 11:40

KIT213 Operating Systems

Semester 2, 2020

Assignment Two – UNIX Shell Programming

(20% of your KIT213 final result)

Due at 3pm Friday Week 13, 16 October 2020

There are three (3) shell programming tasks in this assignment. This is an

individual assignment. You are required to make a directory named

kit213a2 under your home directory (on alacritas), and use kit213a2

as your working directory for this assignment.

Assignment submissions will be marked by the KIT213 tutors on alacritas

(also known as ictteach). If you write and test your scripts elsewhere it's

your responsibility to ensure that your scripts run correctly on the School

UNIX system (alacritas).

Task A

Write a shell script (to run on the Bourne shell) to copy all the files from the

directories named dir1 and dir2 into a new directory named dir3. Time

stamps must be preserved while copying files from one directory into

another.

(1). In the beginning of your script you need to check that the directories dir1

and dir2 exist under the current directory (if not, your script displays an

error message and then exits).

(2). All the three directory names are supplied as arguments, and the new

directory dir3 must not currently exist (ie, the creation of dir3 is part of the

script).

(3). The script first copies all the files from dir1 into dir3.

(4). The script then copies every file from dir2 to dir3 subject to these

conditions: if the file from dir2 is not already present in dir3, or if the dir2

file is newer than the same dir3 file it will overwrite its namesake in dir3.

(5). Remove all the temporary files (if any) at the end of your script.

Page 2 of 10

(6). Your script must generate an output similar to the sample output shown

below.

Hint: You should note that the cp command without any option does not

preserve certain attributes (such as ownership and timestamps).

Your script for this task must be named cp2.sh. Make sure that your script is

user-friendly and follows common sense. The following is a sample output of

the script. The $ is the shell prompt.

$ ./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) their

namesakes in dir3:

a.c

The following sample outputs verify the above result:

$ ls

cp2.sh dir1 dir2 dir3

$ ls -l dir1

total 12

-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 a.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c

$ ls -l dir2

total 12

-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:51 b.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c

$ ls -l dir3

total 16

-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 a.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 b.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:52 file1.c

-rw-r--r-- 1 sxu staff 9 Aug 2 12:53 file2.c

(Continued next page)

Page 3 of 10

Task B

Write a shell script (to run on the Bourne shell) that runs an infinite loop to

monitor which users on a denial list have logged into the UNIX system more

than once.

(1). The denial list must be stored in a text file named user.deny which lists

which users are not allowed multiple logins, specified by one username per

line.

(2). In the beginning of the script you need to check that the user.deny file

exists under the current directory, and if not, your script displays a message

to say so and then exits.

(3). Every 3 seconds, the script must display a warning message to report

users on the denial list who have logged in multiple times. Display the user’s

full name instead of his/her username in the warning message.

(4). It’s unnecessary for your script to handle the situation where a user logs

in and then logs off immediately (i.e. within a 3 second interval).

Your script for this task must be named mlog.sh. The following is a sample

output of the script (It is OK if the script leaves behind temporary files when

it is finally interrupted). The $ is the shell prompt.

$ ./mlog.sh

No user on the user.deny list has multiple logins

No user on the user.deny list has multiple logins

The user John Smith (on the denial list) has logged in more than

once!

The user Adam Jones (on the denial list) has logged in more than

once!

No user on the user.deny list has multiple logins

No user on the user.deny list has multiple logins

The user David Monks (on the denial list) has logged in more than

once!

The user Nick Andrews (on the denial list) has logged in more than

once!

… … … … … … … … … … … … … … … …

Page 4 of 10

Task C

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 about each employee: telephone number (8 digits, first digit nonzero),

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.

(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 not,

your script displays a message and then exits).

(3). Your script (must be named menu.sh) will present a menu of operations

that a user may choose from. Among other tasks, these operations automate

the following four activities:

1. Displaying all current employee records on the screen.

2. Searching for and displaying specific employee record(s) (search all

fields, ignoring case)

3. Adding new records to the records file.

4. Deleting records from the records file.

(4). Your script must produce the following menu:

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

Page 5 of 10

(5). After a 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.

(6). You must validate the employee details when adding a new record.

(7). A valid family name, first name and job title must be alphabetic characters

and/or spaces. The first digit of a valid phone number (of eight digits) must

not be zero. Each telephone number must be unique. A valid department

number must only contain 2 digits.

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.

$ ./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

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

Page 6 of 10

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)

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): (user simply presses Enter without typing in anything)

Phone number not entered

Phone Number (xxxxxxxx): 00123456 (user input)

Invalid phone number

Phone Number (xxxxxxxx): 95437869 (user input)

Phone number exists

Phone Number (xxxxxxxx): 99887766 (user input)

Family Name: abc123 (user input)

Family name can contain only alphabetic characters and spaces

Page 7 of 10

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: if the user answer is y here then the above procedure is repeated)

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)

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.)

(Continued next page)

Page 8 of 10

For All Your Scripts

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 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 a single folder named kit213a2 which contains the

following five (5) files, electronically to the kit213submit folder which has

been created for you in your alacritas account (do NOT create this

kit213submit folder yourself, see below for more details):

cp2.sh, mlog.sh, user.deny, menu.sh, records

(Please make sure that your kit213a2 folder only contains the five files as

listed here, when you are ready to submit them)

To submit your assignment, follow these three steps:

1). Ensure that both your kit213a2 folder and your kit213submit folder

are stored right under your home directory;

2). Run the following commands to copy your kit213a2 folder into your

kit213submit folder (the $ is the shell prompt):

$ cd (This takes you back to your home directory from anywhere)

$ cp -r kit213a2 kit213submit (This copies your assignment folder into your

kit213submit folder. The option -r is important)

$ ls -l kit213submit (This verifies that your assignment folder is now in your

kit213submit folder)

$ ls -l kit213submit/kit213a2 (This verifies that your assignment files are

now in your kit213submit folder)

3). Immediately use your mobile phone to make a screenshot of your secure

shell window (such as PuTTY window) and submit the image file to KIT213

MyLO (under Assessments, then Assignments), as proof of your assignment 2

submission. Use your name and ID number as the image file name, for

example, Smith_John_012345.jpg. If your assignment files are copied into

the kit213submit folder by the deadline, you do not receive any penalty if

this image file is slightly late.

Page 9 of 10

IMPORTANT NOTE: The kit213submit 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

kit213submit folder does not exist, please advise the Unit Coordinator

immediately. Without a kit213submit folder, you will NOT be able to

submit your assignment 2.

If your assignment is late then you should submit your files to the

kit213late folder. The late folder will be created automatically after the

due date and will be available for one week only.

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)

Page 10 of 10

Appendix: KIT213 Assignment Two Marking Scheme

Script 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 their namesakes 2

Script mlog.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 the existence of the file named user.deny 2

Correctly set up an infinite loop 2

Correctly display “No user on the user.deny list has multiple logins”

when this is the case

2

Correctly report the full names of the users (on the denial list) with

multiple logins

2

Script 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 when adding new records 2

Correctly delete records 2

Others (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 (0)

2

Include name, ID, and brief introduction in all scripts 2

Appropriate in-line comments 2

Assignment Total: /40


版权所有:留学生编程辅导网 2018 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。