OS Lab 3
OS Lab 3
System
Lab Manual
Operating System
BCS-132
20-21
PREFACE
This lab manual has been prepared to facilitate the students of Computer Science in studying and
analyzing different operating systems i.e. LINUX or Ubuntu The lab sessions are designed to
improve the abilities of the students by giving hands on experience. After completing the
laboratory exercises, the students will be familiar with the practical issues of the different concepts
explained in the course i.e. real time network configuration and Shell programming
.
PREPARED BY
Lab manual is prepared by Ms. Madiha Wahid, Ms. Mehreen Kamran, Ms. Andleeb Yousaf Khan
and Mr. Majid Shafique under the supervision of Head of Department Dr. Nargis Bibi
GENERAL INSTRUCTIONS
a. Students are required to maintain the lab manual with them till the end of the semester.
b. All readings, answers to questions and illustrations must be solved on the place
provided. If more space is required, then additional sheets may be attached. You may
add screen print to the report by using the ‘Print Screen’ command on your keyboard
to get a snapshot of the displayed output.
c. It is the responsibility of the student to have the manual graded before deadlines as
given by the instructor.
d. Loss of manual will result in re submission of the complete manual.
e. Students are required to go through the experiment before coming to the lab session.
f. Students must bring the manual in each lab.
g. Keep the manual neat clean and presentable.
h. Plagiarism is strictly forbidden. No credit will be given if a lab session is plagiarized
and no re-submission will be entertained.
i. Marks will be deducted for late submission.
j. You need to submit the report even if you have demonstrated the exercises to the lab
instructor or shown them the lab report during the lab session
Contents
Lab 01
Introduction to Linux 2
Lab 02
Introduction to Basic Shell commands 5
Lab 03
Implementing Linux Commands
Lab 03
Summary
Items Description
Course Title Operating System
Lab Title
Implementing Linux Commands
Duration 3 Hours
Operating System Linux Operating System
/Tool/Language
Objective To get familiar with the Linux commands.
Command: date
This command is used to print system date and time.
Example:
o date
Command: cal
"cal" stands for calendar. It displays the calendar of the current month.
Example:
o cal
Command: cat
It displays the content of the file sequentially with no break.
Example:
o cat > file1 (”>” is called Redirection Operator)
It will create the file ‘file1’ and you can enter the text there. Then press “Ctrl+d” for
saving the file. If file1 already exists then it over writes the contents of the file1.
o cat >>file1
This example appends more text to already existing file. Then press “Ctrl+d” for saving
the file.
Command: head
It displays the top part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.
Examples:
o head –n25 file1.txt
This example displays the first 25 lines of the file named “file1.txt”.
Command: tail
It displays the last part/lines of the file. By default it allows 10 lines, but you can change the
settings of the number of lines to be displayed by using –n.
Examples:
o tail –n25 file1.txt
This example displays the last 25 lines of the file named “file1.txt”.
Command: ls -l
This command is use to find permission level of the file.
Command: chmod
Each file in UNIX/LINUX has an associated permission level. This allows the user to
prevent others from reading/writing/executing their files or directories.
If you own a file, you can change its permissions with “chmod”.
Example
o chmod 7 7 7 filename
user group others
Gives user, group and others r, w, x permissions
o chmod 770 filename
Mostly all command gives output on screen or take input from keyboard, but in Linux (and in
other OSs also) it's possible to send output to file or to read input from file.
Command: ls
“ls” command is used to output to screen.
Examples:
o ls > file1
This command put output of ls to file1.
It outputs Linux-commands result (output of command or shell script) to file. Note that if file
already exist, it will be overwritten else new file is created.
o ls >> file1
This command put output of ls at the end of file1.
It output Linux-commands result (output of command or shell script) to end of file. If file
exists, it will be opened and new information/data will be written to end of file, without losing
previous information/data, and if file does not exist, then new file is created.
o ls < file1
This command put contents to command or shell script from the file.
It takes input to Linux-command from file instead of key-board.
Command: sort
“sort” is used to sort the contents in the file.
Examples:
o sort file1
In above example sort command takes input from “file1” file and output of sort command (i.e.
sort_name) is redirected to sort_name file.
Variables in Shell
o System variables - Created and maintained by Linux itself. This type of variable
defined in CAPITAL LETTERS.
o User defined variables (UDV) - Created and maintained by user. This type of variable
defined in lower letters.
Example:
o no=10
It define variable called ‘no’ having value 10.
o vech=Bus
It define variable called 'vech' having value Bus
There are rules for naming variable name (Both UDV and System Variable)
o Don't put spaces on either side of the equal sign when assigning value to variable. e.g.
In following variable declaration there will be no error.
o Variables are case-sensitive, just like filename in Linux. For example:
Example:
o vech=Bus
o echo $vech
It will print the value of vech.
Shell Arithmetic
Examples:
o expr 1 + 3
o expr 2 - 1
By default in Linux if particular command/shell script is executed, it return two type of values
which is used to see whether command or shell script executed is successful or not.
LAB TASKS
Task 1:
Create file name students.txt, gstudent.txt, pgstudents. Enter students’ names in gstudent.txt
and pgstudent.txt. Now create directory having name OSLAB and copy all files to this
directory. Now append the file students.txt with first five sorted names from pstudent.txt and
last five sorted names from gstudent.txt. Then show the contents of sorted names from file
students.txt. Change the permissions of file students.txt read only and both other files read and
execute only.
Now append the file students.txt with first five sorted names from pstudent.txt:
Last five sorted names from gstudent.txt. Then show the contents of sorted names from
file students.txt.:
Task 2:
Define variable x and y with value 20 and 30 and print it on screen, then sum, subtract, divide
these numbers and print it on the screen.
Task 3:
Write a program that will displays the fibanocaii series of any given number. Take the limit
from the user