代写 R C game shell operating system graph software Go COMPSCI 2211b

COMPSCI 2211b
Software Tools and Systems Programming
Assignment #2
Shell Scripts
Posted: Due: Total:
February 8th 2019 February 26th 2019 11:55PM 100 Points (5% of Final Grade)

CS2211 – Software Tools and Systems Programming
Assignment #2
Learning Outcomes
By completing this assignment, you will gain and demonstrate skills relating to: 􏰀 Reading and understanding shell scripts.
􏰀 Creating your own shell scripts.
􏰀 More practice with tr, wc, head, tail, etc.
Instructions
For this assignment, an electronic submission is required through OWL. This submission should include a copy of each shell script you created in plain text format (not in a PDF, word, zip or tar file). Your shell scripts should be named as per the directions given in each question.
All Linux commands and shell scripts given must run correctly on the course server (cs2211b.gaul.csd.uwo.ca) in the Bash shell. A question will only be marked as correct if your output can be replicated on the course server.
You will be assessed on the following:
􏰀 Completion of each question correctly.
􏰀 Following the naming conventions for each shell script.
􏰀 Providing a plain text (.sh) copy of each shell script (not a screenshot, PDF, etc.).
􏰀 Including your full name, student number, etc. in the comments of each shell script as
described in the question.
􏰀 Documenting your code with comments.
􏰀 Having proper error checking in your scripts.
􏰀 Assignment submission via OWL.
1 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
Questions Question 1 (12 Marks)
Consider the following bash script /cs2211/a2/good.sh:
#!/bin/bash
echo
hour=`date +%H`
if [ “$hour” -lt 12 ]; then echo “GOOD MORNING”
elif [ “$hour” -lt 18 ]; then echo “GOOD AFTERNOON”
else
echo “GOOD EVENING”
fi echo
(a) Rewrite this script to not use elif . Make sure you include a plain text copy of the rewritten script named good2.sh in your submission.
(b) Add a comment below the hashbang line explaining in one or two sentences what this script does. Also include your full name and student number.
(c) Add a few lines to the end of this shell script to also output “Thank Goodness It’s Friday” if it is Friday, “It’s Wednesday My Dudes” if it is Wednesday and “Have a Nice Weekend” if it is Saturday or Sunday. For all other days of the week do not output any additional text. You may use any type of conditional statement to add this feature.
Question 2 (22 Marks)
For this question, you will be creating a bash shell script named say.sh that will work like the cowsay and ducksay commands we have seen previously but will allow you to choose the animal. This script takes 1 or more arguments. The first argument is the name of the animal (either cow, duck, cat, dog or frog) and the remaining optional arguments will be the text the animal says in a speech balloon above their head. If no arguments other than the animal name are given, a random line from /cs2211/a2/fortunes.txt should be used. Assume that the number of lines in this file can change (you may not hardcode the number of lines in your script).
Rather than hardcoding your own ASCII art for the animals, use the files cow.txt, duck.txt, cat.txt, dog.txt and frog.txt from the /cs2211/a2 directory. The text from these files should be shown directly and not hardcoded into your script (i.e. if these files change, the art your script outputs should change as well).
You must do error checking and exit with a sensible exit status and error message if no arguments are given or the name of the animal is not one of cow, duck, cat, dog or frog.
2 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
You MUST use a case statement in your script to select between the animals.
You do not have to worry about text wrapping to the next line. Assume all inputs will fit on one line in the console.
Your script is NOT allowed to use the cowsay, ducksay, or fortune commands/scripts.
You should include a comment at the beginning of the file (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least two comments to lines in your script explaining what they do.
Hints
Making the speech bubble may seem like a hard task that requires loops or finding the length of strings, but you are likely over thinking the problem. Consider other options like using the tr command to create a string the same length as the text but with all one character (it might help to read the man page for tr).
Example Input / Output
[ dservos5@cs2211b a2 ] $ say . sh Error: Need at least one argument.
[ dservos5@cs2211b a2 ] $ say . sh unicorn
Error: Animal must be one of cow, duck, cat, dog or frog.
[ dservos5@cs2211b a2 ] $ say . sh cow
< Go take a rest; you deserve it. > −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
\ˆˆ \ (oo)\
( )\ )\/\ ||−−−−w |
|| ||
[ dservos5@cs2211b a2 ] $ say . sh cat
< All your hard work will soon pay off. > −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
\,, \ |\\ //| \ |=6 6=| \ \=. Y .=/
)`( , /\((
||)) /| | | |\ // \| |. .| |/−`
‘” ‘ ‘” ‘
[dservos5@cs2211b a2]$ say.sh frog Hello world, and hello CS2211.
3 of 13

CS2211 – Software Tools and Systems Programming
Assignment #2
< Hello world , and hello CS2211. > −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
\
\ (‘)−=−(‘)
(”)
/ /’−−−−−’\ \
\\ \\ // //
> )/ \−−−/ \( < [dservos5@cs2211b a2]$ say.sh dogWOW. much shell script. so UNIX. such shell. −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
\ /ˆˆ\ \ /00\ \ V\ Y /V
/−\ |\ ||(V
[ dservos5@cs2211b
a2 ] $
say . sh duck
< You are working hard. > −−−−−−−−−−−−−−−−−−−−−−−
\
\ <(o ) (.>/ `−−−’
[ dservos5@cs2211b a2 ] $ say . sh duck
< You are almost there. > −−−−−−−−−−−−−−−−−−−−−−−
\
\ <(o ) (.>/ `−−−’
4 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
Question 3 (22 Marks)
Write a bash shell script named path.sh that takes an even number of arguments (four or more).
These arguments are (x, y) pairs that represent points in a path. That is:
path.sh x1 y1 x2 y2 … xn yn
where x1, y1, … xn, yn are integer values that make up the points (x1, y1) to (xn, yn)
Your script will calculate the following based on this input:
􏰀 The total length of the path.
􏰀 The longest distance between two consequent points in the path
􏰀 The average distance between two consequent points
􏰀 If the path leads exactly back to the start, if not the distance between the starting point and
ending point.
Each calculation should be done with 2 decimal points of precision and the output for each length should be a real number with 2 decimal points of precision.
Your script should contain error checking and exit with an appropriate exit status and error message when an error is encountered. You should check for the following errors at a minimum:
􏰀 If the script is given less than four arguments.
􏰀 If the script is given an odd number of arguments.
􏰀 If any argument to the script is not an integer value.
In the event of an error the only output should be your error message and not any errors from other commands. For example, if there is an invalid integer your script should not output something like “-bash: test: cat: integer expression expected”.
You should include a comment at the beginning of the file (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least two comments to lines in your script explaining what they do.
Hints
You can test if a number is an integer using the test command. The test command will return an exit status of 2 if you attempt to use a non-integer with an operation like -eq. You can read the exit status of the last command with $?.
The test command can not compare real values, but the bc command can and you can use it’s output with test.
The equation to find the distance between two points is: d = 􏰁(x1 − x2)2 + (y1 − y2)2
5 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
Example Input / Output
[ dservos5@cs2211b a2 ] $ Error: Need at least 4
[dservos5@cs2211b a2]$
Error : Need an even number of
path . sh arguments.
path.sh 1 2 3 4 5 6 7 arguments .
[ dservos5@cs2211b Error: cat is not
[ dservos5@cs2211b Error: 3.4 is not
[dservos5@cs2211b
Total path length Longest distance between Average distance between Path leads 2.82 distance
a2]$ path.sh an integer.
a2]$ path.sh an integer.
cat dog
12 3.4
a2]$ path.sh : 2.82
4
1 2 3
two points : two points : from start .
duck fox
5
2.82 2.82
5 5 5 5 0 0 0
5.00 5.00
0 6 5 4 1 −1 0 3 −1 0 7.61
5.33
6 3 5 2 4 6 4 4 2 1 2 0 3 1 4 −1 5 1 6
4.00 2.09
[dservos5@cs2211b a2]$ path.sh 0 0 0 Total path length : 20.00
Longest distance between two points : Average distance between two points : Path leads back to start .
[dservos5@cs2211b a2]$ path.sh −3 −1 Total path length : 26.67
Longest distance between Average distance between Path leads 2.23 distance
two points : two points : from start .
[dservos5@cs2211b a2]$ path.sh 1 6 2 Total path length : 20.92
Longest distance between two points : Average distance between two points : Path leads back to start .
Visualizations of the Example Paths
The following are graphical representations of the paths in the example input / output. Note that your code is not required to produce any graphics, these are simply given to aid your understanding of the example output.
6 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
path.sh 1 2 3 4
path.sh 0 0 0 5 5 5 5 0 0 0
7 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
path.sh -3 -1 0 6 5 4 1 -1 0 3 -1 0
path.sh 1 6 2 6 3 5 2 4 6 4 4 2 1 2 0 3 1 4 -1 5 1 6
8 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
Question 4 (22 Marks)
Write a bash shell script named dobackup.sh that takes no arguments. When run, this shell script will display a list of the directories (and only the directories) in the current working directory with a number beside each. For example, if the current working directory was /usr the output might look like this:
1) bin
2) etc
3) games 4) include 5) lib
6) lib64 7) libexec 8) local 9) sbin 10) share 11) src 12) tmp
Your script should then ask the user to enter a number from the menu and make an archive of this directory in the current working directory using the tar command. We did not go over the tar command in class, so it is up to you to read the man page for tar. You do not need to use compression.
Your script should name the tar file:
-backup-.tar
where is the directory name, is the day of the month (with a leading 0 if one digit), is the current month (with a leading zero if one digit) and is the current 4-digit year. For example, if the directory is named “local” and the current date is January 31st, 2019 the file should be named local-backup-31-01-2018.tar.
Your shell script should keep asking the user for directories to backup until the user inputs a zero. If any invalid number is input an error should be displayed and the user should be asked for a valid directory number again. If no directories are in the current working directory, an error should be displayed and the script should exit immediately with an appropriate exit status.
You should include a comment at the beginning of the file (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least two comments to lines in your script explaining what they do.
Hints
When trying to match a directory name with a number, you do not need to use an array or store the directory names. Rather you can simply loop over the files in the current working directory again and assume they are in the same order.
You can test if a number is an integer using the test command. The test command will return
9 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
an exit status of 2 if you attempt to use a non-integer with an operation like -eq. You can read the exit status of the last command with $?.
Example Input / Output
In a directory with no
[ dservos5@cs2211b a2 ] $
Error : No directories in current working directory .
In a directory with one or more subdirectories:
[dservos5@cs2211b ̃]$ a2/dobackup.sh 1) a2
2) clmystery
3) cs2211 2018
4) dir1
5) dir2
6) gidtestdir
7) inodetest
8) linktest
9) mybackupdir
10) testdir
Backup which directory? (0 to exit): cat
Error: not a valid directory number.
Backup which directory? (0 to exit): −10
Error: not a valid directory number.
Backup which directory? (0 to exit): 11
Error: not a valid directory number.
Backup which directory? (0 to exit): 2
Created backup f i l e clmystery−backup−08−02−2019.tgz Backup which directory? (0 to exit): 10
Created backup f i l e testdir −backup−08−02−2019.tgz Backup which directory? (0 to exit): 4
Created backup f i l e dir1−backup−08−02−2019.tgz Backup which directory? (0 to exit): 0
Good bye.
subdirectories :
dobackup . sh
10 of 13

CS2211 – Software Tools and Systems Programming Assignment #2
Question 5 (22 Marks)
Write a bash shell script named cipher.sh that encrypts and decrypts files based on a key given as an argument. A valid key is an ordering of the letters in the alphabet. That is a string containing the letters A to Z in any order that is not alphabetical. You can assume that all keys given to the script are valid (do not need to do error checking on keys).
Your script will use the tr command to translate a given file or text using this key such that all occurrences of the letter A in the text are replaced with the first letter in the key, all occurrences of the letter B in the text are replaced with the second letter in the key, and so on up to and including Z and the last letter in the key.
Your script should deal with capitalization by first converting everything in the file or text to upper case before encrypting. You may assume that the key will be given in all upper case.
Your script will work in two different modes, interactive or non-interactive, based on the number of arguments given to it. Your script will take the following arguments:
cipher.sh mode key output [target]
where mode is either “-e” or “-d” for encrypt or decrypt respectively, key is a string containing the 26 letters in the alphabet in some order, output is the file the result will be stored in and target is an optional argument that contains the path to the file to be encrypted or decrypted. If the target argument is not given, the text will be read in from the standard input.
Your script should not create or edit any files other than the output file.
You must do error checking and exit with a sensible exit status and error message in the following cases:
􏰀 The mode argument is not “-e” or “-d”
􏰀 More than 4 or less than 3 arguments are given. 􏰀 The target file (if given) does not exist.
You should include a comment at the beginning of the file (under #!/bin/bash) that contains your name, student number and a one or two sentence description of what the script does. Also add at least two comments to lines in your script explaining what they do.
Example Input / Output
[ dservos5@cs2211b a2 ] $ cipher . sh Error : Need 3 or 4 arguments
[ dservos5@cs2211b a2 ] $ cipher . sh −a ZYXWVUTSRQPONMLKJIHGFEDCBA out Error: First argument must be −e or −d
[ dservos5@cs2211b a2 ] $ cipher . sh −d ZYXWVUTSRQPONMLKJIHGFEDCBA out bad fail name . txt Error: Target file does not exist
11 of 13

CS2211 – Software Tools and Systems Programming
Assignment #2
[ dservos5@cs2211b a2 ] $ cipher . sh −e ZYXWVUTSRQPONMLKJIHGFEDCBA This is my first line of text.
I hope my script works!
Hit Ctrl−D to send a EOF to exit.
[ dservos5@cs2211b a2 ] $ cat out GSRH RH NB URIHG ORMV LU GVCG.
R SLKV NB HXIRKG DLIPH!
SRG XGIO−W GL HVMW Z VLU GL VCRG.
out
[ dservos5@cs2211b a2 ] $ cipher . sh −d ZYXWVUTSRQPONMLKJIHGFEDCBA decrypt out
[ dservos5@cs2211b a2 ] $ cat decrypt THIS IS MY FIRST LINE OF TEXT.
I HOPE MY SCRIPT WORKS!
HIT CTRL−D TO SEND A EOF TO EXIT.
dservos5@cs2211b a2 ] $ cipher . sh −d NSZFXTOJCVQHLAGRIWMPYBKEUD /cs2211/week5/textbook . txt
[ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 1 22 23 24 25 26 27 28 29 30 31 32
[
[
[ 1 2 3 4 5 6 7 8
dservos5@cs2211b a2 ] $ cat output . txt | cipher . sh −d NSZFXTOJCVQHLAGRIWMPYBKEUD decrypt . txt
dservos5@cs2211b a2 ] $ cat decrypt . txt
REAL−LIFE EXAMPLES UNIX CONCEPTS ARE SIMPLE, BUT THEY ARE ALSO ABSTRACT, AND
IT S OFTEN NOT OBVIOUS WHY A CERTAIN FEATURE IS HANDLED IN A PARTICULAR WAY. THE MASTERY OF THIS OPERATING SYSTEM REQUIRES A CLEAR UNDERSTANDING OF THESE CONCEPTS. I HAVE MADE SURE THAT THE KEY FEATURES ARE EXPLAINED CLEARLY TO REVEAL BOTH THEIR DESIGN CONSIDERATIONS AND THEIR RELEVANCE IN THE REAL WORLD. YOULL FIND THAT MANY EXAMPLES OF THIS TEXT REFER TO REAL−LIFE SITUATIONS.
dservos5@cs2211b a2 ] $ cat output . txt
WXNH−HCTX XENLRHXM YACE ZGAZXRPM NWX MCLRHX, SYP PJXU NWX NHMG NSMPWNZP, NAF
CPM GTPXA AGP GSBCGYM KJU N ZXWPNCA TXNPYWX CM JNAFHXF CA N RNWPCZYHNW KNU. PJX LNMPXWU GT PJCM GRXWNPCAO MUMPXL WXIYCWXM N ZHXNW YAFXWMPNAFCAO GT PJXMX ZGAZXRPM. C JNBX LNFX MYWX PJNP PJX QXU TXNPYWXM NWX XERHNCAXF ZHXNWHU PG
WXBXNH SGPJ PJXCW FXMCOA ZGAMCFXWNPCGAM NAF PJXCW WXHXBNAZX CA PJX WXNH KGWHF. UGYHH TCAF PJNP LNAU XENLRHXM GT PJCM PXEP WXTXW PG WXNH−HCTX MCPYNPCGAM.
SGPJ N YMXWM NAF RWGOWNLLXWM OYCFX PJXWX NWX LNCAHU PKG ZNPXOGWCXM GT YACE YMXWM: PJGMX KJG YMX CPM ANPCBX PGGHM, NAF GPJXWM KJG FXBXHGR PGGHM YMCAO MJXHH MZWCRPM NAF PJX YACE MUMPXL ZNHH HCSWNWU. PJCM SGGQRWGSNSHU PJX GAHU GAX GT CPM QCAFNFXIYNPXHU NFFWXMMXM SGPJ GT PJXMX MXOLXAPM SYP JNM N MCDX
PJNP CM ZGABXACXAP PG WXNF NAF ZNWWU.
PJX YMXW ZNPXOGWU CM MXWBXF SU PJX TCWMP 11 ZJNRPXWM, KJCZJ CM NFXIYNPX TGW NA CAPWGFYZPGWU YACE ZGYWMX. PJX FXBXHGRXW CM N MJXHH GW MUMPXLM RWGOWNLLXW KJG NHMG AXXFM PG QAGK JGK PJCAOM KGWQ, MNU, JGK N FCWXZPGWU CM NTTXZPXF KJXA N TCHX CM ZWXNPXF GW HCAQXF. TGW PJXCW SXAXTCP, PJX CACPCNH ZJNRPXWM ZGAPNCA MRXZCNH SGEXM PJNP RWGSX QXU ZGAZXRPM. PJCM NWWNAOXLXAP
MJGYHFAP NTTXZP PJX SXOCAAXW, KJG LNU IYCXPHU COAGWX PJXMX RGWPCGAM. YACE MJCAXM PJWGYOJ ZJNRPXWM 1 6 , 1 7 , NAF 1 8 , MG PJXMX ZJNRPXWM NWX ZGLRYHMGWU WXNFCAO TGW MUMPXLM RWGOWNLLXWM.
MPWGAO HXNWACAO NCFM PJX RXFNOGOCZNH NCFM NWX N MPWGAO TXNPYWX GT PJCM SGGQ. PJXU JNBX BNWCGYM ANLXM, TGW XENLRHX, AGPX, ZNYPCGA, NAF PCR. C ZGAMCFXW HCAYE PG SX NA CLRGWPNAP LXLSXW GT PJX YACE TNLCHU, MG C JNBX MXRNWNPXHU JCOJHCOJPXF HCAYE TXNPYWXM YMCAO PJX RXAOYCA NM CFXAPCTCXW.
C FGAP NOWXX KCPJ PJX NRRWGNZJ NFGRPXF SU LNAU NYPJGWM GT PWXNPCAO XNZJ MJXHH CA N MXRNWNPX ZJNRPXW. CAMPXNF, C JNBX FCMZYMMXF QXU ZGAZXRPM YMCAO LNCAHU PJX SNMJ MJXHH. FXBCNPCGAM NWX NFFWXMMXF SU MXRNWNPX NMCFXM TGW PJX Z MJXHH, QGWA, NAF SGYWAX MJXHHM.
12 of 13

CS2211 – Software Tools and Systems Programming
Assignment #2
9 BOTH A USERS AND PROGRAMMERS GUIDE THERE ARE MAINLY TWO CATEGORIES OF UNIX
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 C SHELL , KORN, AND BOURNE SHELLS .
USERS: THOSE WHO USE ITS NATIVE TOOLS, AND OTHERS WHO DEVELOP TOOLS USING SHELL SCRIPTS AND THE UNIX SYSTEM CALL LIBRARY. THIS BOOKPROBABLY THE ONLY ONE OF ITS KINDADEQUATELY ADDRESSES BOTH OF THESE SEGMENTS BUT HAS A SIZE THAT IS CONVENIENT TO READ AND CARRY.
THE USER CATEGORY IS SERVED BY THE FIRST 11 CHAPTERS, WHICH IS ADEQUATE FOR AN INTRODUCTORY UNIX COURSE. THE DEVELOPER IS A SHELL OR SYSTEMS PROGRAMMER WHO ALSO NEEDS TO KNOW HOW THINGS WORK, SAY , HOW A DIRECTORY I S AFFECTED WHEN A FILE IS CREATED OR LINKED. FOR THEIR BENEFIT, THE INITIAL CHAPTERS CONTAIN SPECIAL BOXES THAT PROBE KEY CONCEPTS. THIS ARRANGEMENT SHOULDNT AFFECT THE BEGINNER, WHO MAY QUIETLY IGNORE THESE PORTIONS. UNIX SHINES THROUGH CHAPTERS 1 6 , 1 7 , AND 1 8 , SO THESE CHAPTERS ARE COMPULSORY READING FOR SYSTEMS PROGRAMMERS.
STRONG LEARNING AIDS THE PEDAGOGICAL AIDS ARE A STRONG FEATURE OF THIS BOOK. THEY HAVE VARIOUS NAMES, FOR EXAMPLE, NOTE, CAUTION, AND TIP. I CONSIDER LINUX TO BE AN IMPORTANT MEMBER OF THE UNIX FAMILY, SO I HAVE SEPARATELY HIGHLIGHTED LINUX FEATURES USING THE PENGUIN AS IDENTIFIER.
I DONT AGREE WITH THE APPROACH ADOPTED BY MANY AUTHORS OF TREATING EACH SHELL IN A SEPARATE CHAPTER. INSTEAD, I HAVE DISCUSSED KEY CONCEPTS USING MAINLY THE BASH SHELL. DEVIATIONS ARE ADDRESSED BY SEPARATE ASIDES FOR THE
13 of 13