UNX510 Professor: Shahdad
Page 1 of 18
UNX510 – Unix BASH Shell Scripting Assignment 3
Objective: To gain experience with advanced scripting techniques.
Due date: August 2, 2021 by midnight
Value: 10% of final course mark
Specifications: This is a continuation of Assignment2 which add some new feature to showDir script. Write a utility called showDir2 that satisfies the following requirements:
1. Usage: showDir2 [ dir-name ]
2. showDir2 displays the permission of a directory and all the directories in the directory’s absolute path. showDir2 will also show other information about the directories, as the cursor is moved up and down the list of directory names. showDir2 will also facilitate changing permission of the directories, assuming that you are the owner of the appropriate directory or the superuser.
3. showDir2 will accept one argument, the name of a directory, including the path if required.
4. If there is no argument specified, then the current directory will be the default.
5. If there is more than one argument specified, there will be an error message and the script terminated (exit status 1) with no full-screen display.
6. If the specified Dir_Name is not a valid, existing directory, there will be an error message and the script terminated (exit status 1) with no full- screen display.
7. Please see the examples for the exact display format, and match this format as closely as possible. Note that the screen is cleared before full-screen output is displayed. There are 3 spaces between the permission groups, and between the permission groups and the Dir_Name. There is one space between the permission within a permission group.
8. Ignore the situation of a very deep directory structure, which would require a multi-screen display.
9. Do not let output lines wrap to the next line, ensure that all lines are truncated to the width of the screen size.
10. Once your program is running, you do NOT have to take into account changes in screen size. You also do NOT have to take into
UNX510 Professor: Shahdad Page 2 of 18
account changes performed from another process, such as changes in permission of the displayed directories or changes in the displayed directory structure.
11. On initial display, the cursor will be on the first character of the specified (or default) Dir_Name. Information for this directory will also be displayed.
12. There are three lines showing the valid keys accepted during execution of the showDir2 command, on the fourth, third, and second lines from the bottom of the terminal window, regardless of the terminal window size used.
13. After the initial display, the valid keys available are the following. Note that there should be no “enter” key required after any of them, and that they are lower case:
o “k” will move the cursor to the line containing the Dir_Name above the current line. Also, information for the new directory level will be displayed, instead of the previous directory level. This key will have no affect if the cursor is already on the top-most Dir_Name, which will be the root.
o “j” will move the cursor to the line containing the Dir_Name below the current line. Also, information for the new directory level will be displayed, instead of the previous directory level. This key will have no affect if the cursor is already on the bottom-most Dir_Name, which will be the specified (or default) directory.
o “h” will move the cursor to the field immediately to the left of the current field. This key will have no affect if the cursor is already on the “r” permission position for “owner”. The fields are the nine permission fields, and the Dir_Name.
o “l” will move the cursor to the field immediately to the right of the current field. This key will have no effect if the cursor is already on the right-most field, which will be the Dir_Name.
o “r” will set read permission to the current Dir_Name, for owner, group, or other, depending on which field the cursor is on. If the cursor is on any field other than one of the three “r” fields, then the “r” key will be ignored.
o “w” will set write permission to the current Dir_Name, for owner, group, or other, depending on which field the cursor is on. If the cursor is on any field other than one of the three “w” fields, then the “w” key will be ignored.
o “x” will set execute permission to the current Dir_Name, for owner, group, or other, depending on which field the cursor is on. If the
UNX510
Professor: Shahdad Page 3 of 18
14. ignored.
o
o
o
cursor is on any field other than one of the three “x” fields, then the “x” key will be ignored.
“-” will remove a permission to the current Dir_Name, for owner, group, or other, depending on which field the cursor is on. If the cursor is on any field other than one of the nine permission fields, then the “-” key will be ignored.
“q” will terminate the showDir2 script. Note that the screen is not cleared, and the command prompt will appear on the bottom line of the terminal window.
Any keys entered, other than the valid keys above, will be
15. If the user doesn’t have authority to change a permission, then the “r”, “w”, “x”, and “-” keystrokes will be ignored. If the permission could be changed, then the output display will reflect the new permission.
16. When moving down or up, the cursor will be on the same field on the new line as on the current one.
17. If showDir2 produced full-screen output, it must end with an exit status of zero. If there was an error, then there is no full-screen output, and the exit status should be one.
18. Ensure that your script is self-contained in one script file, called showDir2
19. Don’t do any argument verification or any processing
in showDir2 other than that specified above. If you’re interested in adding more capabilities, please do so in a different script.
20. “Flashing” of the screen, during cursor movement, is OK. For example, when the cursor is moved up or down, you may redraw the entire screen.
(BONUS mark) you can eliminate the flashing, and making the program more pleasant to use.
NOTE:
1. Ensure that your output format matches the following exactly as the screenshots in the next page
2. Important: The files in the sample outputs are available, please use them for testing. Your output should duplicate the outputs EXACTLY as shown (though some links, sizes, and date-time stamps might change), or marks will be liberally redirected to /dev/null
UNX510 Professor: Shahdad Page 4 of 18
Assignment 3: showDir2 Sample Screen Shots
After changing to the sample directory, there is a display of the current path, a listing of the current directory, and samples of the required error messages (file file1 and directory dir1 exist, file2 and dir2 do not exist). No other error messages should be produced, even if they “roll off” the screen. The screen size is 80 columns by 24 rows:
==> cd ~unx510/sample.dir1/testdir1/this*
==> pwd
/home/unx510/sample.dir1/testdir1/this.is.an
.incredibly.long.directory.name.to.s
ee.what.will.happen.with.incredibly.long.dir
ectory.names
==> ls -l
total 4
-rw——- 1
cars
drwxr-xr-x 2
dir1
-rw——- 1
file1
==> showDir2
file2 is not
==> showDir2
Usage: showDir2 [ dir-name ]
==> showDir2 dir2
dir2 is not a valid directory name
==> showDir2 dir1 dir2
Usage: showDir2 [ dir-name ]
unx510 users 445 Mar 13 02:24
unx510 users 15 Mar 13 02:33
unx510 users 0 Mar 14 2019
file2
a valid directory name
file1 file2
==>
UNX510 Professor: Shahdad Page 5 of 18
Entering showDir2 dir1 produced the following. Note that the cursor is over the “d” of “dir1”:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x
rwx r-x r-x
rwx r-x r-x
rwx r-x r-x
unx510
sample.dir1
testdir1
this.is.an.incredibly.long.directory.name.to
.see.what.wi
rwx r-x r-x dir1
Links: 2 Owner: unx510 Group: users
Size: 15 Modified: Mar 13 02:33
Valid keys: k (up), j (down): move between
h (left), l (right): move
Dir_Names
between permission
r, w, x, -: change permission;
q: quit
Hitting j and x had no effect. Hitting k twice (with no
Owner Group Other Dir_Name
—– —– —– ——–
UNX510 Professor: Shahdad Page 6 of 18
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx r-x r-x testdir1
Links: 4 Owner: unx510 Group: users
Size: 229 Modified: Feb 22 2015
rwx r-x r-x this.is.an.incredibly.long.directory.name.to .see.what.wi
rwx r-x r-x dir1
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting l had no effect. Hitting h five times produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
UNX510 Professor: Shahdad Page 7 of 18
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx r-x r-x testdir1
Links: 4 Owner: unx510 Group: users
Size: 229 Modified: Feb 22 2015
rwx r-x r-x
this.is.an.incredibly.long.directory.name.to
.see.what.wi
rwx r-x r-x dir1
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting x had no effect. Hitting w produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
UNX510 Professor: Shahdad Page 8 of 18
rwx rwx r-x testdir1
Links: 4 Owner: unx510 Group: users
Size: 229 Modified: Feb 22 2015
rwx r-x r-x
this.is.an.incredibly.long.directory.name.to
.see.what.wi
rwx r-x r-x dir1
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting k four times produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
Links: 18 Owner: root Group: root Size: 4096 Modified: Aug 21 2019
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx rwx r-x testdir1
UNX510 Professor: Shahdad Page 9 of 18
rwx r-x r-x this.is.an.incredibly.long.directory.name.to .see.what.wi
rwx r-x r-x dir1
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting w and k had no effect. Hitting l twice, then hitting j five times, then hitting – produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx rwx r-x testdir1
rwx r-x –x
this.is.an.incredibly.long.directory.name.to
.see.what.wi
UNX510 Professor: Shahdad Page 10 of 18
Links: 3 Owner: unx510 Group: users Size: 43 Modified: Mar 22 2015
rwx r-x r-x dir1
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move between permission
r, w, x, -: change permission;
q: quit
Hitting h six times produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx rwx r-x testdir1
rwx r-x –x this.is.an.incredibly.long.directory.name.to .see.what.wi
Links: 3 Owner: unx510 Group: users Size: 43 Modified: Mar 22 2015
rwx r-x r-x dir1
UNX510 Professor: Shahdad Page 11 of 18
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting h had no effect. Hitting q produced the following (Note that the prompt is displayed after the bottom line of the showDir2 display:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x
rwx –x –x
rwx r-x r-x
rwx rwx r-x
rwx r-x –x
home
unx510
sample.dir1
testdir1
this.is.an.incredibly.long.directory.name.to
.see.what.wi
Links: 3 Owner: unx510 Group: users Size: 43 Modified: Mar 22 2015
rwx r-x r-x dir1
UNX510 Professor: Shahdad Page 12 of 18
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit ==>
r, w, x, -: change permission;
Increasing the screen size to 32 rows by 96 columns, then entering showDir2 produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x
rwx –x –x
rwx r-x r-x
rwx rwx r-x
rwx r-x –x
home
unx510
sample.dir1
testdir1
this.is.an.incredibly.long.directory.name.to
.see.what.will.happen.with.i
Links: 3 Owner: unx510 Group: users
Size: 43 Modified: Mar 22 2015
UNX510 Professor: Shahdad Page 13 of 18
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
r, w, x, -: change permission;
q: quit
Hitting the keys hhhrkhh- produced the following:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx r-x r-x testdir1
Links: 4 Owner: unx510 Group: users
Size: 229 Modified: Feb 22 2015
rwx r-x r-x this.is.an.incredibly.long.directory.name.to .see.what.will.happen.with.i
UNX510 Professor: Shahdad Page 14 of 18
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
Hitting q produced the following. Note that the prompt is still displayed after the bottom line of the showDir2 display:
Owner Group Other Dir_Name
—– —– —– ——–
r-xr-xr-x/
rwx r-x r-x home
rwx –x –x unx510
rwx r-x r-x sample.dir1
rwx r-x r-x testdir1
Links: 4 Owner: unx510 Group: users
Size: 229 Modified: Feb 22 2015
rwx r-x r-x this.is.an.incredibly.long.directory.name.to .see.what.will.happen.with.i
UNX510 Professor: Shahdad Page 15 of 18
Valid keys: k (up), j (down): move between
Dir_Names
h (left), l (right): move
between permission
q: quit
r, w, x, -: change permission;
==>
UNX510 Professor: Shahdad Page 16 of 18
Assignment 3: Hints and Tips
Here are some hints that may help you with this assignment:
1. Of course, most of the hints for Assignment 2 still apply.
2. Create the initial display, ignoring user input, and ignoring the extra information required for the “current” level. Include the “Valid commands” line near the bottom of the screen.
3. Set up a loop that does the following until a “q” is entered. Don’t worry yet about “enter” being required for user input:
Inside the loop, display the screen of information using your code from the previous step. Keep track of the level being displayed so that you can use the current level variable to determine when to display the additional information.
At the end of the loop, get user input. If “k” or “j” is entered, update the current level variable, but only if this wouldn’t produce a level too high or too low for the number of levels being displayed.
4. Add the ability to read user input without “enter” being required.
5. When there is a valid request to change a permission, I would suggest that you don’t try to determine beforehand if the user has authority to do this. Instead, just go ahead and execute the appropriate “chmod” command, redirecting any error messages to /dev/null
Assignment 3: Marking Scheme
The marking scheme for Assignment 2 will be as follows:
1. If your script can’t run with results similar to the examples correctly, with output EXACTLY duplicating the sample output format, or does not allow changing of permission when the user has the correct authority,then the assignment may be a resubmit.
2. Marks will be deducted for errors in the following areas:
UNX510
Professor: Shahdad
Page 17 of 18
a. b.
c. d. e.
f.
g. h.
Assignment
Comments as specified: course, family name, etc. Output format as specified:
heading and “Valid commands” lines
correct files and directories displayed
alignment as specified
cursor displayed and moved correctly
detailed info displayed and moved correctly handles different window sizes correctly
Error messages as specified, and redirected properly.
Proper exit status, terminal characteristics reset to normal. Handles pathnames at any directory level, including relative, relative-to-home, and absolute pathnames.
Handles command keys and “Ctrl-c” properly and ignores other “normal” keys.
Handles attempts to move up, down, left, or right too far.
Uses /tmp directory and $$ for temporary files, and temporary files deleted (if used).
Submission
1. Assignment3isdueonAugust2,2021@11:59pm.
2. SubmittedassignmentsMUSTcontainthestudent’sOWNsolutiontothe
assignment.
3. Your assignment MUST run on the Matrix server.
4. Ensure that your script is self-contained in one file, called showDir2
5. At the beginning of your script, include comments with the following information
formatted neatly, and reflecting accurate information for you.
# Assignment 3
# Course:
# Family Name:
# Given Name:
# Student Number:
# Login name:
UNX510
Student
John
123-456-789
jstudent
Shahdad Shariatmadari
August 2, 2021
# Professor:
# Due Date:
#
# I declare that the attached assignment is my own work in
# accordance with Seneca Academic Policy. No part of this
# assignment has been copied manually or electronically from
# any other source (including web sites) or distributed to
# other students.
UNX510 Professor: Shahdad Page 18 of 18
What to Submit:
1) Please submit the script that you developed during this Assignment.
2) Please submit a recorded-video which explains how you did the lab and demonstrate your solutions. This will give everyone the opportunity to present their solution. Please record a video (2~10 minutes) with the following contents:
Introduce yourself
Show/demonstrate how your assignment works.
Explain your code (walkthrough), how you design it ( a quick/detailed walk-through of the programming code , running the commands in Matrix)
Speak about challenges that you faced during this Assingment.
Evaluate yourself. Have you implemented all requirements of the lab? how do you
evaluate yourself out of 10 for this Assingment?
NOTE: You can record the video using some screen-capture software (like OBS : https://obsproject.com/ ) or your cellphone. To submit the video:
o You can upload the video on the youtube (you may make it unlisted) and submit the link here.
o You can also directly upload the video to the BB.