CST8102 Lab 8:
Iterative Scripts
If you plan on downloading/printing this, note:
0.1Updates
[ Apr 8 ; 17:25 ]Improved the example formulti_ln.sh
, added hints
Updated the checking program tov1.03
Run the checkers updaterto update it.
[ Apr 6 ; 16:14 ]Added 3 examples formulti_ln.sh
[ Apr 2 ; 19:36 ]Updated the checking program tov1.02- improved the feedback messages.
Run the checkers updaterto update it.
[ Apr 1 ; 17:14 ]Added a note on being able to run the checking program at any time in this lab ; added examples for invalid range inmul_table.sh
[ Mar 27 ; 19:09 ]Updated the checking program tov1.01- Added task completion %, added missing test for multi_ln.sh and improved some feedback messages.
Run the checkers updaterto update it.
[ Mar 25 ; 13:10 ]Indicated thatmulti_ln.sh
does not accept options.
[ Mar 23 ; 15:57 ]Added a second example tomul_table.sh
; corrected typo and broken link.
1Due Date and Deliverables
Due Date: Written on Brightspace underActivities->Assignments
Prerequisites
- All class notes since the beginning of term up-to Week 10 notes (including)
- Ubuntu 20.04 installed in a VM from Lab 01
- An ability toREAD ALL THE WORDSto work efficiently.
Deliverables
- Complete the tasks in theShelland use the checking program to evaluate if you’ve done your work correctly.
- The file
a08.bz2
uploaded to Brightspace as instructed below.
- Each script file you created in this lab, uploaded as .txt file
Evaluation
- Weight:4.5%of the final mark.
- Reminder: Your cumulative lab component grade (and theory component grade) needs to be over 50% to pass the course.
2Purpose of this Lab
- Gain experience using some of the Linux commands below:
Week 1 |
Week 2 |
Week 3 |
Week 4 |
Week 5 |
Weeks 6-7 |
---|
date
|
more
|
fgrep (grep ) |
stat
|
free
|
groupadd
|
man
|
head
|
paste
|
id
|
file
|
groupmod
|
ls
|
tail
|
cp
|
whoami
|
df
|
groupdel
|
rm
|
cut
|
mv
|
groups
|
fdisk
|
useradd
|
less
|
history
|
find
|
chmod
|
mkfs
|
usermod
|
cat
|
help
|
locate
|
umask
|
mkswap
|
userdel
|
touch
|
pwd
|
|
|
mount
|
chsh
|
wc
|
cd
|
|
|
umount
|
su
|
sort
|
mkdir
|
|
|
swapon
|
newgrp
|
echo
|
rmdir
|
|
|
swapoff
|
passwd
|
|
tree
|
|
|
|
|
Weeks 8-9 |
Weeks 10-11 |
---|
chown
|
shift
|
chgrp
|
exit
|
gpasswd
|
if /case statements |
alias
|
test /[ ,]
|
unalias
|
read
|
export
|
while /for /until loops |
env
|
functions |
set
|
arrays |
Learn how to troubleshoot error messages
Understand the importance of accuracy and character case in Linux
Understand special directories such as: your home directory,..
(two dots),.
(one dot),\
Practice using absolute and relative paths
Practice passing multiple arguments and options to commands
Gain experience using the following meta-characters:
>
2>
>>
- ‘&>’
|
(pipe)
;
hello
(input redirection)
*
,?
,[ ]
,[! ]
(GLOB patterns)
"
,'
(Double/single quotes)
\
(Escape/ignore meta characters)
{ }
(Brace expansion)
Understand the use ofsystem filessuch aspasswd
,shadow
,group
,useradd
,shells
(located directly or under the/etc
directory) and system directories such as/etc/skel
.
Understand how to create and use environment variables
This assignment is based on your weekly class notes, and the commands you learned in class.
Remember toREAD ALL THE WORDSto work efficiently and not waste time.
Why is reading all the words more time efficient?
Taking action with incomplete information, will give you incorrect results. You will need to read the whole information again, and retake action.
Your ability to be accurate and thorough is evaluated in the quizzes and test questions in this course.
3THE 15 MINUTES RULE
Your time as a student is valuable. If you come up against a tough problem and make no progress in 15 minutes despite best efforts, don’t keep going. Change things up:
- Read the task slowly again. Maybe you missed a hint or a requirement.
- Get help from your lab professor
- Get help from classmates.
- Take a break.
- Put it on hold and work on something else.
Often, the solution to the problem is a small thing you overlooked. Spending three hours working on a problem only to discover that you typed a comma instead of a period isnotgood use of your time.
Spending too much time on a problemhappens to all of us, and we need to watch out for it when it happens.
4Preparation: Downloading the Checking Program
If you didn’t download and use thecheckers_update
inlab 3, refer to the notes fromlab 3to download it.
4.1Run the checkers updater
Thecheckers_updater
will retrieve the latest version of checking programs for you.
You may be asked to run it again when starting a new lab, or when a new update to the checking program is released.
To run it, type:
~/checkers_updater
You may see output similar to below:
$ ~/checkers_updater Checking for updates... -> a08checker updated successfully Finished checking for updates. $
The output above shows that a checking programa08checker
was downloaded and ready for you to use.
4.2Run the checking program
You can run it by typing:
$ ~/a08checker
More information on using the checking program can be found inlab 3.
This checking program creates directory calledsubmission
under thelab08
directory. When you finish the assignment tasks below, submit all the files in that directory.
4.3How to complete the tasks
Keep notes for every command name used in this course, giving its name, an explanation of what it does, and a short example. Quizzes and tests will require you to remember these commands.
You will now be asked to complete tasks using the commands you learned. Refer to your class notes if you need, or to the examples that took place in the theory class.
If you see error messages, or not get the results you expect to get, read the error message carefully and/or think why things don’t work.
- Did you type the command accurately? Check for typos.
- How does the command work? Check the manual page of the command
- Review your notes and/or the demonstration that took place in class
Keep in mindThe 15 Minutes Rule
Reach out to your lab professor for help if in doubt.
To help you, your lab professor will need to know:
- The task number you are working on
- The exact command you were typing
- The error message (or unexpected output) you see
5Document Your Scripts
You must document your scripts with comment lines before you submit it. Script comment lines start with the comment orhashtagcharacter#
and extend to the end of the line.
Script documentation will be evaluated manually, the checking program can not evaluate those parts of your scripts.
After the 3 lines of the script header, add a comment with:
To show understanding of your work, you will need toheavily documentyour script. For every command in the script (after the 3 lines script header) that is not identical to the previous command, document what the command is doing.
For example:
# display an introduction message echo Starting the backup process # show the start time of the backup date # create a directory for the backup mkdir backup # backup the file into the new directory cp 1.txt backup/1.txt # show the end time of the backup date # displaying a message on the backup completion echo The backup was completed successfully echo Have a good day!
6Tasks
Do the tasks in order, some tasks rely on previous tasks.
Please use a single terminal. Commands in multiple terminals may not show as correct in the checking program until you closed them.
6.1A Warning on Plagiarism
There is no group work in this course.
If your script is found be unoriginal, your professor is required to report it as a possible violation of the Academic Integrity Policy (AA48), and a hearing will take place.
All students involved in the act are reported. Both the source and the copy.
Do your own assignments; do not copy, and do not share your answers.
6.2Working Directory
lab08
Create a directory namedlab08
directly underyourHOME directory, and enter it.
You may nowrun the checking programto verify your work.
Remain inside thelab08
directory for the rest of the lab, unless instructed otherwise.
6.3Iterative Scripting
You can at anytimerun the checking programwhile working on the scripts in this lab.
mul_table.sh
Create a script that generates a multiplication table between a range of values
Examples:
$ ./mul_table.sh 1 5 1 2 3 4 5 2 4 6 8 10 3 6 9 12 15 4 8 12 16 20 5 10 15 20 25 $
$ ./mul_table.sh 5 7 25 30 35 30 36 42 35 42 49 $
$ ./mul_table.sh 5 10 25 30 35 40 45 50 30 36 42 48 54 60 35 42 49 56 63 70 40 48 56 64 72 80 45 54 63 72 81 90 50 60 70 80 90 100 $
The script will not display any additional output or messages
The script will use precisely and only a single tab (\t
) escape character between the numerical values
(Check the manual page of theecho
command for relevant options for displaying it neatly as above)
The script will be able to handle the following errors:
In case of any of those errors, the script will terminate with an error code (pick one) and display an appropriate error message in thestandard errorchannel
Assumptions:
You can assume that if script receives two arguments, both are numbers
You can assume the output will fit on the terminal’s width
When you are done, proceed todocument your script
.
multi_ln.sh
In Lab 5A, you were asked to create multiple names (hard links) to the same file.
Theln
command, however, doesn’t support this feature, you had to run it for each file.
Create a script calledmulti_ln.sh
that will receive 2 or more arguments:
multi_ln.sh TARGET LINK_NAME(S)...
The script will create multiple hard links as needed.
The script will not display any output or messages on (correct) execution.
The script will be able to handle the following errors:
Incorrect number of arguments
The TARGET isn’t an existing file
Any LINK_NAME file already exists
In this situation, display an error message about the problematic file (include which file causes the error), then proceed with the next link (if there is one). When done, still terminate with an error code, as a problem occurred.
In case of any of those errors, the script will produce an error code (pick one). Error message will be displayed in thestandard errorchannel.
Example 1:
$ touch 1.txt $ ls -i 1.txt 1180131 1.txt $ ./multi_ln.sh 1.txt 2.txt $ echo $? 0 $ ls -i 1.txt 2.txt 1180131 1.txt 1180131 2.txt
Example 2:
$ touch a $ ls -i a 1179923 a $ ./multi_ln.sh a b c d e $ echo $? 0 $ ls -i a b c d e 1179923 a 1179923 b 1179923 c 1179923 d 1179923 e
Example 3: (existing files)
$ touch v x y $ ls -i v x y 1180137 v 1180138 x 1180139 y $ ./multi_ln.sh v w x y z (displays an error message about the file x) (displays an error message about the file y) $ echo $? 1 $ ls -i v w x y z 1180137 v 1180137 w 1180138 x 1180139 y 1180137 z
Note that links with the namesw
andz
were created in example 3.
Assumptions:
The script will only support hard-links. The script does not need to support any options. Only the arguments specified above.
You can assume that you have sufficient permissions to the pathnames provided as arguments
You can assume that the provided pathnames are valid
Hints:
Click the hints to reveal, as needed.
How can I iterate over arguments?
I’m iterating over the arguments but the TARGET argument is removedWhen some LINK_NAME file exists, how to return an exit code without terminating the script?