0% found this document useful (0 votes)
25 views11 pages

OS LAB 3 - 2k24

Uploaded by

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

OS LAB 3 - 2k24

Uploaded by

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

Operating Systems

Laboratory # 03

22/1/2024
Superior University Lahore
Mahira Zainab
Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running
Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the
terminal. Before understanding shell scripting, we have to get familiar with the following
terminologies: -
 Kernel
 Shell
 Terminal

What is Kernel?
The kernel is a computer program that is the core of a computer’s operating system, with complete
control over everything in the system. It manages the following resources of the Linux system –
 File management
 Process management
 I/O management
 Memory management
 Device management etc.

Operating System is a system software. Kernel is system software which is part of an operating system.
Operating System provides interface between user and hardware. Kernel provides interface between
applications and hardware.
It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only responsible for the
development of the Linux kernel.

Complete Linux system = Kernel + GNU system utilities and libraries + other management scripts +
installation scripts.
What is Shell?
A shell is a special user program that provides an interface for the user to use operating system
services. Shell accepts human-readable commands from users and converts them into something
which the kernel can understand. It is a command language interpreter that executes commands read
from input devices such as keyboards or from files. The shell gets started when the user logs in or
starts the terminal.
In Linux, there are two major types of shells −
 Bourne shell − If you are using a Bourne-type shell, the $ character is the default prompt.
 C shell − If you are using a C-type shell, the % character is the default prompt.
The Bourne Shell has the following subcategories −
 Bourne shell (sh)
 Bourne Again Shell (bash)
 Korn shell (ksh)
 Z shell (zsh)
The different C-type shells follow –
 C shell (csh)
 TENEX/TOPS C shell (tcsh)

What is a terminal?
A program which is responsible for providing an interface to a user so that he/she can access the
shell. It basically allows users to enter commands and see the output of those commands in a text-
based interface. Large scripts that are written to automate and perform complex tasks are executed
in the terminal.
Shell Scripting
As a shell can also take commands as input from file, we can write these commands in a file and can
execute them in shell to avoid this repetitive work. These files are called Shell Scripts or Shell
Programs. A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to get
started with it.
A shell script comprises the following elements –
 Shell Keywords – if, else, break etc.
 Shell commands – cd, ls, echo, pwd, touch etc.
 Functions
 Control flow – if..then..else, case and shell loops etc.

Creating a Shell Script


1. Create a file named test.sh using the touch/vi command.
2. Put the shebang line first and then add the commands.
3. Make the script executable using chmod +x text.sh.
4. Execute the file using ./test.sh.

Assume we create a test.sh script. Note all the scripts would have the .sh extension. Before you add anything
else to your script, you need to alert the system that a shell script is being started. This is done using
the shebang construct. For example –

#! /bin/bash

This tells the system that the commands that follow are to be executed by the Bourne Again Shell
(bash).
It's called a shebang because: -
 The # symbol is called a hash.
 The ! symbol is called a bang.

To create a script containing these commands, you put the shebang line first and then add the
commands –
#! /bin/bash
pwd
ls
How to run a script
Make sure script has execute permission rwx
Run using
 ./script.sh
 /path/script.sh
 bash script.sh
Ctrl+C to terminate
Ctrl+z to stop
Variables
Name Syntax Example
Defining Variables VAR_NAME=value VAR1="Zara"
VAR2=100
Accessing Values echo $VAR_NAME #!/bin/bash

NAME="Zara"
echo $NAME
Constant Variables readonly VAR_NAME #!/bin/bash
 Once you defined a
variable and don’t want to NAME="Zara"
change it until end of the readonly NAME
script.
NAME="Ali"

Arrays
Defining an Array array_name=(value1 ... valuen) myArray=(1 2 ali
“hello world”)
Get values from an array ${array_name[index]} echo
"${myArray[0]}"
echo
"${myArray[1]}"
Access all the items in an  ${array_name[*]} echo "First
array  ${array_name[@]} Method: ${ myArray
[*]}"
echo "Second
Method: ${ myArray
[@]}"
Length of array "${# array_name[*]}" echo "${#myArray[*]}"

Linux - Shell Basic Operators


There are various operators supported by each shell. We will discuss in detail about Bourne shell
(default shell).

We will now discuss the following operators −


 Arithmetic Operators
 Relational Operators
 Logical operators
Bourne shell didn't originally have any mechanism to perform simple arithmetic operations but it uses
external program,
1. expr
2. $((expression))
The following points need to be considered while adding −

 There must be spaces between operators and expressions. For example, 2+2 is not correct; it
should be written as 2 + 2.
 The complete expression should be enclosed between ` `, called the backtick.

Arithmetic Operators
The following arithmetic operators are supported by Bourne Shell.
 + (Addition)
 - (Subtraction)
 * (Multiplication)
 / (Division)
 % (Modulus)
 = (Assignment)
 == (Equality)
 != (Not Equality)
It is very important to understand that all the conditional expressions should be inside square braces
with spaces around them, for example [ $a == $b ] is correct whereas, [$a==$b] is incorrect.
Relational Operators
Bourne Shell supports the following relational operators that are specific to numeric values. These
operators do not work for string values unless their value is numeric.

-eq Checks if the value of two operands are equal or not; if yes, then the condition
becomes true.
-ne Checks if the value of two operands are equal or not; if values are not equal, then
the condition becomes true.
-gt Checks if the value of left operand is greater than the value of right operand; if yes,
then the condition becomes true.
-lt Checks if the value of left operand is less than the value of right operand; if yes,
then the condition becomes true.
-gt Checks if the value of left operand is greater than or equal to the value of right
operand; if yes, then the condition becomes true.
-le Checks if the value of left operand is less than or equal to the value of right operand;
if yes, then the condition becomes true.
It is very important to understand that all the conditional expressions should be placed inside square
braces with spaces around them. For example, [ $a <= $b ] is correct whereas, [$a <= $b] is incorrect.
Logical operators

&& condition1 && condition2 If both conditions are true then true else false

|| condition1 || condition2 If any of the condition is true then true


Conditional Statement
The if...else statements
If else statements are useful decision-making statements which can be used to select an option from
a given set of options.
Linux Shell supports following forms of if…else statement −
 if...fi statement
 if...else...fi statement
 if...elif...else...fi statement
Most of the if statements check relations using relational operators.

Linux - Shell Loop Types


A loop is a powerful programming tool that enables you to execute a set of commands repeatedly.
We will examine the following types of loops available to shell programmers-
 The while loop
 The for loop
 The infinite loop

while loop
The while loop enables you to execute a set of commands repeatedly until some condition occurs. It
is usually used when you need to manipulate the value of a variable repeatedly.
Syntax
while command
do
Statement(s) to be executed if command is true
done

for loop
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
 for j in Apple Mango Orange
 for i in {1..20}
infinite loop
Syntax
while true
do
Statement(s) to be executed.
sleep 2s
done
Lab Tasks
Task 1: Marks: 3
1. How to define variable x with value 10 and print it on screen?
2. How to define variable xn with value Ali and print it on screen?
3. How to print sum of two numbers, let's say 6 and 3?
4. How to define two variable x=20, y=5 and then to print division of x and y?
5. Modify above and store division of x and y to variable called z.

Task 2: Marks: 3
Take two numbers from user and Check whether an integer is odd or even?

Task 3: Marks: 4
The list of values (Mon, Tue, Wed, Thu and Fri), store the values in a variable, and use the
variable in the for loop and print the weekdays.

***********

You might also like