[go: up one dir, main page]

0% found this document useful (0 votes)
67 views13 pages

OS Lab 3

The document is an instructor's lab manual for an Operating Systems course. It provides instructions and objectives for three labs on basic Linux commands, shell commands, and implementing additional commands. The labs are designed to give students hands-on experience with key Linux and shell concepts. The manual provides detailed descriptions of common commands like date, cal, cat, head, tail, ls, chmod. It also covers redirection, shell scripts, variables, and arithmetic. The lab tasks section provides a sample multi-step task for students, involving creating and manipulating files, directories and permissions.

Uploaded by

Laiba Babar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views13 pages

OS Lab 3

The document is an instructor's lab manual for an Operating Systems course. It provides instructions and objectives for three labs on basic Linux commands, shell commands, and implementing additional commands. The labs are designed to give students hands-on experience with key Linux and shell concepts. The manual provides detailed descriptions of common commands like date, cal, cat, head, tail, ls, chmod. It also covers redirection, shell scripts, variables, and arithmetic. The lab tasks section provides a sample multi-step task for students, involving creating and manipulating files, directories and permissions.

Uploaded by

Laiba Babar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Operating

System
Lab Manual

Fatima Jinnah Women University,


Rawalpindi

Operating System
BCS-132

Mr. Majid Shafique


Lab Engineer
majidshafique@fjwu.edupk

Instructor Laboratory Manual

DEPARTMENT OF COMPUTER (BCS)


Fatima Jinnah Women University, Rawalpindi
www.fjwu.edu.pk

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.

Implementing 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.

The permission levels are

“r” Read only


“w” Write
“x” execute

I n case of directory, “x” grants permission to list directory contents.

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

Gives the user read, write and execute.


Gives group members read and execute.
Gives others no permissions.

Using numeric representations for permissions:


r = 4; w = 2; x = 1; total = 7

Redirection of Input and Output

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

This command sorts the contents in the file1.

o sort < file1 > sort_names


o cat sort_names

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.

o tr "[a-z]" "[A-Z]" < file1 > cap_names


o cat cap_names
tr command is used to translate all lower case characters to upper-case letters. It takes input
from “file1” file, and tr's output is redirected to cap_names file.

Shell Scripts and C programming

Variables in Shell

To process our data/information, data must be kept in computers RAM memory.


RAM memory is divided into small locations, and each location had unique number
called memory location/address, which is used to hold our data. Programmer can give a
unique name to this memory location/address called memory variable or variable
In Linux (Shell), there are two types of variable:

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.

To define UDV use following syntax


Syntax: variable name=value
'value' is assigned to given 'variable name' and Value must be on right side = sign.

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 Variable name must begin with Alphanumeric character or underscore character


(_), followed by one or more Alphanumeric character.

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:

To print or access value of UDV (User defined variables)

Example:
o vech=Bus
o echo $vech
It will print the value of vech.

Shell Arithmetic

Use to perform arithmetic operations.

Examples:

o expr 1 + 3
o expr 2 - 1

These examples print the result.

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.

(1) If return value is zero (0), command is successful.


(2) If return value is nonzero, command is not successful or some sort of error executing
command/shell script.

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.

Create file name students.txt, gstudent.txt, pgstudents:

Enter students’ names in gstudent.txt and pgstudent.txt:


Create directory having name OSLAB:

Copy all files to OSLAB directory:

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.:

Change the permissions of file students.txt read only:

Both other files read and execute only.

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

You might also like