[go: up one dir, main page]

0% found this document useful (0 votes)
33 views17 pages

OS Lab Journal 3-007

The document is a lab manual for an Operating Systems lab course. It details 4 tasks completed by a student: 1) Write programs to demonstrate Linux variables, 2) Write a calculator program, 3) Write if/else programs to compare numbers, 4) Write a case program to output different fruits. The student's solutions and outputs are shown. The objective is to understand Linux shell programming variables, wildcards, expressions and control structures.

Uploaded by

Danish Rashid
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)
33 views17 pages

OS Lab Journal 3-007

The document is a lab manual for an Operating Systems lab course. It details 4 tasks completed by a student: 1) Write programs to demonstrate Linux variables, 2) Write a calculator program, 3) Write if/else programs to compare numbers, 4) Write a case program to output different fruits. The student's solutions and outputs are shown. The objective is to understand Linux shell programming variables, wildcards, expressions and control structures.

Uploaded by

Danish Rashid
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/ 17

Bahria University, Lahore Campus

Department of Computer Sciences


Lab Journal 03
(Spring 2023)

Course: Operating System Lab Date:


Course Code: CSL – 320 Max Marks: 20
Faculty’s Name: Lab Engineer: iqra ashraf

Name: Danish Rashid Enroll No: 03-135212-007

Objective(s) :
To understand LINUX shell programming variables, wildcards, expressions and control
structures.

Lab Tasks :

Task 1: Write the output of programs for LINUX variables.

Solution:
Linux Variables
Lab Manual Operating System | CSL-320

Output

Task 2: Write a program to calculate the addition, subtraction, multiplication and division of
numbers.

Solution:
Calculator Program
Lab Manual Operating System | CSL-320

Output

Task 3.1: Write a program that compares two numbers if a is greater than b it displays “a is

greater than b”, otherwise it displays that ‘a is not equal to b’.

Solution:
If else program
Lab Manual Operating System | CSL-320

Task 3.2: Write a program that compares two numbers check whether the numbers are equal, a

is greater than b or a is less than b.

Solution:
If else program
Lab Manual Operating System | CSL-320

Task 4 : Write a program using “case” that inputs a fruit from the user and displays “Apple pie”

on the input of apple, “I like banana” on the input of banana and “New Zealand famous for kiwi”

on the input of kiwi.

Solution:
Case program

Output
Lab Manual Operating System | CSL-320

Lab Grading Sheet :


Max
Obtained
Task Mark Comments(if any)
Marks
s
1. 5
2. 5
3. 5
4. 5

Total 20 Signature

Note : Attempt all tasks and get them checked by your Lab. Instructor
Lab Manual Operating System | CSL-320

Lab 03: LINUX Shell Programming - I

Objective(s):
To understand LINUX shell programming variables, wildcards, expressions and control
structures.

Tool(s) used:
Ubuntu, VIM Editor

Introduction

Shell programming is a group of commands grouped together under single filename. After logging
onto the system a prompt for input appears which is generated by a Command String Interpreter
program called the shell. The shell interprets the input, takes appropriate action, and finally prompts
for more input. The shell can be used either interactively - enter commands at the command prompt,
or as an interpreter to execute a shell script. Shell scripts are dynamically interpreted, NOT
compiled.

Common Shells

C-Shell - csh

The default on teaching systems Good for interactive systems Inferior programmable
features.

Bourne Shell

bash or sh - also restricted shell – bsh (The Bourne Again Shell) It was written by Steve
Bourne. Over the years the original Bourne Shell has been expanded, but it remains the basic
shell provided in many commercial versions of Linux.

Korn Shell

It was written by David Korn This shell extended many features of Bourne Again Shell
and added many new features.

Thomas C-Shell - tcsh

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

The TC Shell performs the same functions as Bourne Again Shell. It is an interactive
command line interpreter as well as for high level programming languages.

Shell Keywords

echo, read, if fi, else, case, esac, for , while , do , done, until , set, unset, readonly, shift,
export, break, continue, exit, return, trap , wait, eval ,exec, ulimit , umask.

General Shell Terminologies

The shebang line or hashbang#!

The “shbang” line is the very first line of the script and lets the kernel know what shall
will be interpreting the lines in the script. The shbang line consists of #! Followed by the
full pathname to the shell, and can be followed by options to control the behavior of
shell.

Example

#!/bin/bash

Comments

Comments are descriptive material preceded by a # sign. They are ineffect until the end
of a line and can be started anywhere on the line.

Example

#This text is not interpreted by the shell.

FIRST “HELLO WORLD” SHELL PROGRAM

Step 1 Open VIM with the filename.sh extension.

Step 2 Write the Hello World Program

#!bin/bash
#YOUR FIRST HELLO WORLD PROGRAM
echo ‘Hello World!’

Step 3 Execution of SHELL Script:

By using change mode

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

command
$ chmod u + x hello.sh
$ ./hello.sh

Shell Variables

A variable is something to which we assign a value. The value assigned could be a


number, text, or any data type.

Rules

 A variable name is any combination of alphabets, digits and an underscore.


 No commas or blanks are allowed within a variable name.
 The first character of a variable name must either be an alphabet or and
underscore.
 Variables names should be of any reasonable length.

Shell Variables

1. PATH - Directory paths to search for commands.


2. HOSTNAME - The name of the computer.
3. USER - The user id of the user running this shell.
4. SHELL - The shell currently is used.
5. TERM - The type of terminal being used.
6. PS1 - The prompt to print when then shell is ready for another command.

Task 1 Write the output of the below programs.


i) #!/bin/bash
#Variable Assignment & Accessing
echo "Variable Name : "
Name="Operating System"
echo $Name

OUTPUT

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

ECHO Statement

Similar to the output statement. To print output to the screen, the echo command is used.
Syntax: Echo “String” (or) echo $ b (for variable).
Example: echo "What is your name?"

READ Statement

To get the input from the user.


Syntax: read x y (no need of commas between variables)

Reading user input: The read command takes a line of input from the user and assigns
it to a variable(s) on the right-hand side. The read command can accept multiple
variable names. Each variable will be assigned a word.

ii) #!/bin/bash
#Input from user
echo "Enter your name"
read NAME
echo "Enter your age"
read AGE
echo "Enter your enrollment"
read ENROLLMENT
echo "Hello $NAME, Your age is : $AGE Your enrollment is :
$ENROLLMENT"

OUTPUT

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

iii) #!/bin/bash
#readonly Variables
echo "Readonly Variables"
Name=”David”
readonly Name
Name=’John’
OUTPUT

iv) #!/bin/bash
echo "Unset Variables : "
Name=”John”
unset Name
echo $Name
OUTPUT

Wildcards

There are some characters that are evaluated by the shell in a special way. They are
called shell meta characters or “Wildcards. These characters are neither number nor
letters.

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

Example

*,?,[ ],$

$$echo $$ -- It represents process ID Number, or PID of the current shell

The following table shows a number of special variables that can be used in shell scripts.

Variable Description
$0 The filename of the current script
$n These variables correspond to the arguments with which a script is
invoked.
$# The number of arguments supplied to a script.
$* All arguments are double quoted. If a script receives two
arguments $* is equivalent to $1,$2
$@ All arguments are individually double quoted, equivalent to $1,$2
$$ Shows the number of current shell.
$! The process number of last background command.

v) #!/bin/bash
#Unix Special Commands
echo "File Name = $0"
echo "First Parameter = $1"
echo "Second Parameter = $2"
echo "Quoted Values = $@"
echo "Quoted Values = $*"
echo "Total number of parameters = $#"
OUTPUT

vi) #!/bin/bash
#Special Commands
echo "File Name = $0"
echo "First Parameter = $1"
echo "Second Parameter = $2"
echo "Quoted Values = $@"
echo "Quoted Values = $*"
echo "Total number of parameters = $#"
echo $?

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

OUTPUT

EXPRESSION Command

Arithmetic Operations

To perform all arithmetic operations. The Bourne shell does not support arithmetic.
LINUX/Linux commands must be used to perform calculations.

Syntax:
var=$(expr $value1 + $value2)

Task 2 Write a program for calculating addition, subtraction, multiplication and division of two
numbers.

Program

OUTPUT

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

Operators

The Bourne shell uses the built-in test command operators to test numbers and strings.

Example
Equality:
= string
!= string
-eq number
-ne number

Logical:
-a and
-o or
! not
Relational:
-gt greater than
-ge greater than, equal to
-lt less than
-le less than, equal to
Arithmetic:

+, -, \*, /, %

Control Structures

Unix Shell supports conditional statements which are used to perform different actions
based on different conditions. Two decision making statements are mentioned below:

 if…else statements
 case…esac statements

if..fi Statements

The if construct is followed by a command. If an expression is to be tested, it is


enclosed in square brackets. The then keyword is placed after the closing parenthesis.
An if must end with a fi.

Syntax:
if [ expression ]
then

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

Statements to be executed if true


fi

if..else...fi Statements

The if…else…fi statements is the next form of control statements that allow shell to
execute statements in more controlled way and making decision in two ways.

Syntax
if [ expression ]
then
Statement(s) to be executed if true
else
Statement(s) to be executed if true
fi

Task 3.1 Write a program that compares two numbers if ‘a’ is greater than ‘b’ it displays “a is
greater than b”, otherwise it displays that ‘a is not equal to b’.
Code

OUTPUT

if…elif…elseif…fi Statement

The if…elif…fi statement is to make correct decision out of several conditions.

Syntax

if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2]
then
Statement(s) to be executed if expression 2 is true

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

elif [ expression 3]
then
Statement(s) to be executed if expression 3 is true
else
Statement(s) to be executed if no expression is true
fi

Task 3.2 Write a program that compares two numbers check whether the numbers are equal, a is
greater than b or a is less than b.

Code

OUTPUT

case…easc Statement

You can handle multiple if…elif statements to perform a multiway branch. However, this
is not the best solution, especially when all the branches depend the value of single
variable.

Syntax

case “word” in
“pattern1”)
Statement(s) to be executed if pattern1 matches;;
“pattern2”)
Statement(s) to be executed if pattern1 matches;;
“pattern3”)
Statement(s) to be executed if pattern1 matches;;

Department of Computer Science, BULC


Lab Manual Operating System | CSL-320

esac

Task 4 Write a program using “case” that inputs a fruit from the user and displays “Apple pie”
on the input of apple, “I like banana” on the input of banana and “New Zealand famous
for kiwi” on the input of kiwi.

OUTPUT

Department of Computer Science, BULC

You might also like