0% found this document useful (0 votes)
1 views

Lab 3

Uploaded by

mugheesahmad571
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)
1 views

Lab 3

Uploaded by

mugheesahmad571
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/ 12

Khawaja Fareed University of Engineering & Information

Technology
Rahim Yar Khan
Department Of Computer Engineering

Operating System Lab

Lab 03: Shell Scripting


Lab Instructor: Ms. Aisha Naseer
Objective:

 What is Shell?
 Syntax of Shell Scripting.

What is a Shell?

A shell is a command-line interface that allows users to interact with the


operating system by typing commands. It acts as an intermediary between the
user and the kernel of the operating system. The shell interprets user commands
and executes them, facilitating tasks such as file management, program
execution, and system configuration.

What is Shell Scripting?

Shell scripting is a way to automate repetitive tasks and manage system


operations by writing scripts for the shell (command-line interface). Shell
scripts are sequences of commands written in a text file and executed by the
shell.

Why Use Shell Scripting?

 Automate repetitive tasks.


 Manage system processes.
 Schedule jobs and backups.
 Combine and run multiple commands efficiently.

Syntax Sensitivity of Shell Scripting

Shell scripting is case-sensitive, meaning that variable names, commands, and


keywords must be used consistently in their exact case. It also requires precise
spacing around syntax elements, such as brackets in conditional statements .

For example, in an if condition, the spaces around the brackets are crucial:
“if [ condition ]; then ... fi” is correct, whereas “if[condition];then ... fi” will
produce an error.

Unlike Java, where the structure is more rigid, shell scripting demands careful
attention to spacing and formatting to ensure that commands and variables are
interpreted correctly.
Important Points while writing scripts:
Following are some important points while writing a script:

1. # is used to write comments in your script

2. Whenever a semicolon (;) is placed between two


commands, shell will treat them as separate commands. So
if you want to write all the commands in your shell script
in one line place a semicolon in between them.

3. A script may start with the line #!/bin/bash to tell your


interactive shell that the program which follows should be
executed by bash.(Note not compulsory in Kali Linux
other distributions require it).

4. “echo” is used to print anything like text, variables on the


terminal/screen.

Types of variables in Shell:


In Linux shell there are three types of variables:

1. User defined or shell variables

2. System or Environment variables

3. Parametric variables

Rules for defining variables:


1. Variable name must begin with Alphanumeric character or
underscore character, followed by one or more
Alphanumeric character.

2. Don’t put spaces on either side of the equal sign when assigning value
to variable.

3. Variables are case-sensitive, just like filename in Linux.

4. You can define NULL variable as


var=

or

var=""

5. Do not use ?,* etc, to name your variable names.

Example of Shell Script:

How to write Shell Script:

First open a command terminal you can open it by pressing Ctrl+Alt+T


on your keyboard. Or by clicking this icon.:
Next create a directory(folder) using command.

(mkdir followed by directory name)

To verify if it is created or not use command ls.

Now navigate to this directory and Create a file.

Via command : touch filename.sh. or via nano filename.sh to create it and


immediately edit it.

after using nano command write your script:


Save it by pressing Ctrl+S.

Then run script by running command bash filename.sh.

It prints Hello World in my case


Write this script and run it.

Lab activities:

Activity 1.1:
Write a shell script that takes two numbers as input from
the user and prints the sum of those numbers.
(hint: use “read” to take user input).
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Activity 1.2:
Write a shell script that multiplies two numbers
provided by the user and displays the result.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------

If-else statements in Shell:

Syntax

if [ expression ]
then
Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi //ending of if.

Example
The above example can also be written using the if...else statement as
follows:
a=10
b=20

if [ $a -eq $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi

If-elif… fi Statements:

Syntax

if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ] // (elif means else-if as same in Java)

then
Statement(s) to be executed if expression 2 is true
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 //ending of if

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.

For example, following operators will work to check a relation between 10


and 20 as well as in between "10" and "20" but not in between "ten" and
"twenty".
Assume variable a holds 10 and variable b holds 20 then

Operator Description Example

Checks if the value of two operands are equal or


-eq [ $a -eq $b ] is not true.
not; if yes, then the condition becomes true.

Checks if the value of two operands are equal or


-ne not; if values are not equal, then the condition [ $a -ne $b ] is true.
becomes true.

Checks if the value of left operand is greater than


-gt the value of right operand; if yes, then the condition [ $a -gt $b ] is not true.
becomes true.

Checks if the value of left operand is less than the


-lt value of right operand; if yes, then the condition [ $a -lt $b ] is true.
becomes true.

Checks if the value of left operand is greater than or


-ge equal to the value of right operand; if yes, then the [ $a -ge $b ] is not true.
condition becomes true.

Checks if the value of left operand is less than or


-le equal to the value of right operand; if yes, then the [ $a -le $b ] is true.
condition becomes true.

Activity 1.3:
Create a shell script that takes three numbers as input
and displays the largest number.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Activity 1.4:

Write a shell script that takes input from user and check if a
number is even or odd.

------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------

Activity 1.5:

Create a simple calculator using if-elif…elif……..fi

------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
-------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
--------------------------------------------------------------------------------

You might also like