You are on page 1of 7

CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

Prerequisites
a) Boot up your CentOS Linux Box and open Terminal

Deliverables
 A MS-Word file of all screenshots and Qs and Tasks, to be uploaded in Week9 DropBox
 Total 10 points ( you lose one point for each wrong asnwer)
 Due date: as written on Dropbox

Follow below instructions and create the scripts, make sure it runs properly as instructed.

Reminder: Bash Script Comparison Operators (I added arrows in next pages screenshots so
you could distinguish them) for scripting you need to remember them
CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

Part 1: File Directory Operators

Create a directory called W10 in your home directory

a) In W10, create a script file called autofile.sh , this script creates Dir1 and f1.txt in your
current directory automatically. Also it checks that the Dir1 and f1.txt exists or not before
creation. Enter below script code in the editor. Make sure you give execute permission
to script and execute it as below:

Directory

File

$ ./autofile.sh

b) What is the purpose of –d for in first if block above?

c) What is the purpose of –f for in second if block above?


CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

d) Now complete the script the way to create below files and directories automatically
when you run it.

e) Make sure it works as desired and Run ls –l to show all your file are created
(Screenshot here) + ( Screen shot of your script)

f) Are all files and directories created?

Part 2
Login Script (String comparison)

This script ask for username from input, and verify that it matches with “bob” as username. If
username is correct it give “Username is OK!” or it notifies user that username is incorrect.

Enter below script, save as login.sh


CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

Now add another IF-Block to the script in order to ask for password and verify if the password
is “123” and give message “Password OK!” or “Incorrect Password!” to user.

Screen shot of your script and execution here:

Part 3

While Loop)

As discussed before, loops help us to do a task over and over

a) Enter below script and save it as counter.sh and run it using:

$ ./counter.sh

b) It uses while loop to count from 1 to 10. Screen shot of output here
CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

c) Now modify the script so you can pass the value of counter using positional parameters ($1)
e.g.

$ ./counter.sh 20

As such it counts upto 20 , and another example:

$ ./counter.sh 35

As such it counts up to 35

Hint: this is the part you need to modify in counter.sh script.

d) make sure it works and put your screenshot of results here

Part 4

FOR Loop)

a) Modify counter.sh script to use for loop instead of while. To do so you need to change the
while loop with a for loop as you see below:

b) It uses for loop to count from 1 to 10. Screen shot of output here
CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

c) Now modify the script so you can pass the value of counter using positional parameters ($1)
e.g.
$ ./counter.sh 20
so it counts upto 20 , or
$ ./counter.sh 35
so it counts up to 35
d) make sure it works and put your screenshot of results here ()

Hint: this is the part you need to modify in counter.sh script.

Q1: What is the benefit of FOR loop compare to WHILE loop?

Q2: What is the benefit of WHILE loop compare to FOR loop?

Q3: write down the numerical comparison operators below


CST8207_Linux Operating system I Algonquin College – Winter 2020

Assignment 9 by: Arsalan

Challenge [has no mark] (Just for Practice): modify the script in FOR loop the way it shows 7 time
table similar to below:

 Shutdown your VM gracefully


 If you had any question ask you Lab Professor

You might also like