Make a subdirectory of your home/SYST13416 called assignment and then make it your pwd. In the assignment
Question:
Make a subdirectory of your home/SYST13416 called "assignment" and then make it your pwd.
In the assignment directory make a file called "assign". This will be your assignment shell script. Ensure others cannot read this file, remove the r permission for group.
Copy the phonebook file from /tmp/phonebook so it is in your pwd. Cre. ate a backup of it, call it phonebook.bk.
Copy /tmp/appendfile so it is in your pwd. The file looks as follows:
Edit the file using vi so that your first name and last name replace the FNAME LNAME text. You may need to change the security setting to do t. his.
Now, edit the script assign to do the following scenarios. You will find it works best if you do it iteratively, not all at once. DO one function at a time, test it/debug and fix it and then move to next:
- If you simply call the script without any parameters, it will ask the user for the LAST name of the person to pull the information for. You will display this record and exit the shell script with return code 1. If you do not find the record, provide an error message and such as "This Last Name was not found, sorry" and exit the script with return code 2.
- If you call the script with option -f , then this means the script will prompt the user for the FIRST name of the person you are looking for. If you find that person, display ONLY their phone extension (hint, use cut command). Exit with return code 3.
- If the script has option -c, then the system will make a campus phonebook for the user. It will prompt the user for the campus code such as NH. Look carefully where that campus code is in the file and cre. ate a regex that matches it. Your script will find all the people in that campus and cr. eate a file with the name of the campus.book. So, for NH it would be NH.book. Put this file in your pwd, but cr. eate a link to it which you put in your /tmp/`whoami` directory (the whoami is replaced by your username).
- If the script uses option -a, then prompt the user for a filename and then append that file to the phonebook. This will be used to add yours and my record to the phonebook. Then, exit the script.
- Otherwise, for each separate word that is passed to the script as a positional parameter, search for that regex int the file and append the records to a file you will call result. If you don't find the name, add a line to result indicating that you could not find it.
After you are done with the script, cre. ate a link to the file result and put in your /tmp/`whoami` directory.
Note: the code to run the regular expressions needed for this assignment is no more than standard grep. If you find yourself using extended regular expressions and/or awk, you are likely doing something more complicated than need be. Do not take code and copy code from someone else without knowing exactly how it works. You may be challenged by the instructor to explain your code. Not being able to explain code you wrote, would be automatic "F".
To exercise your script, run the following commands, capture the screen images and submit this file into the drop box.
At a high level, here is the test for your script:
- bash assign (5 marks)
- bash assign -f (5 marks)
- bash assign -c (5 marks)
- bash assign -a (5 marks)
- bash assign Buchner Cheryl Joel Elvis
(10 marks)
Tips:
- ignore case for your greps
- be careful to embed your strings with " "
- Use $? To check return codes and determine if a command worked
- Look at the phonebook file carefully (in vi?) to see how it is structured so that you can grep the correct pattern/regex
- Use echo and read to wri. te and read from terminal session
- Use exit to exit a script. If you put a number after it, the value of the number will be $?. If not, $? will be 0. So ,exit 2 will return $? value of 2
- Use cut to select fields or characters in the output
- Use pipe to make more complicated filters
- You can redirect or append your output
Details for your submission:
- bash assign
- shell will prompt for a last name
- enter the last name Pangborn
- type echo $? the result should be 2
- type bash assign again
- enter the last name Trudeau
- Shell should present a message that it was not found
- Type echo $? The result should be 1
- Type whoami
It should look something like the following
- Take a screen shot of above steps on your screen and paste below.
- bash assign -f
- shell will prompt for a first name
- enter the first name Mark
- The system should only get phone numbers for persons with Mark (not city Markham or last name Markovski). The result should be something like the following. You are welcome to make it look better.
- -
- Next if you type echo $? The result should be 3.
- Last, type whoami so I can see this is your screen
Take a screen shot of above steps and paste below.
- bash assign -c
- shell will tell you it is making a campus phonebook
- enter LE
- the system will make a campus phonebook called LE.book
- type ls and take a screen shot and paste it here
- if you type cat LE.book, you will see only people in the LE campus (the second fields will only LE)
- Type the command whoami so I can see this is your session
- Take a screen shot of above steps and paste below.
- bash assign -a
- The system should prompt you for the name of the file you want to append
- Type the filename appendfile (or whatever you called the file)
Take a screen shot of above steps and paste the result below
- bash assign Buchner Cheryl Joel Elvis
Smith and Roberson Business Law
ISBN: 978-0538473637
15th Edition
Authors: Richard A. Mann, Barry S. Roberts