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

Lab 4

Uploaded by

mugheesahmad571
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab 4

Uploaded by

mugheesahmad571
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

These are some fundamental points to remember when writing shell scripts:

Important Points:

1. Comments:
o Use # to write comments in your script.
o Comments are useful for explaining what the script does, making it easier to
understand and maintain.
o Example:

bash
Copy code
# This is a comment explaining the next line
echo "Hello, World!"

2. Command Separation:
 A semicolon (;) separates multiple commands on the same line.
 This allows you to execute multiple commands in a single line within your script.
 Example:

bash
Copy code
echo "Hello"; echo "World"

 The above example will run both commands separately, printing "Hello" and
"World".

3. Shebang (#!/bin/bash):

 The line #!/bin/bash is called a shebang and is used at the beginning of the
script to specify which interpreter should execute the script.
 While it's not always required (like in Kali Linux), using the shebang ensures that
the script runs with the specified shell (e.g., Bash) across different Linux
distributions.
 Example:

bash
Copy code
#!/bin/bash
echo "This script runs with Bash!"

4. Printing with echo:

 The echo command is used to display text, variables, or messages to the


terminal or screen.
 It is a basic command in shell scripting for producing output.
 Example:

bash
Copy code
echo "Welcome to shell scripting!"

Types of Variables in Shell:

1. User-Defined or Shell Variables:


o These are variables created and defined by the user in the script or terminal.
o They are used to store data or values that can be referenced later in the script.
o Example: my_name="John"
2. System or Environment Variables:
o These are variables predefined by the system and used by the operating system
and shell.
o Common environment variables include $HOME, $PATH, and $USER.
o They usually store information about the system environment or user
preferences.
3. Positional (Parametric) Variables:
o These are special variables used to pass command-line arguments to the script.
o For example, $1, $2, etc., represent the first, second, and subsequent
arguments passed to a script.
o $0 represents the name of the script itself.

Rules for Defining Variables:

1. Variable Naming:
o Variable names must begin with an alphanumeric character (a-z, A-
Z) or an underscore (_), followed by one or more alphanumeric
characters.
o Example: name1, _myVariable

2. No Spaces Around the Equal Sign:


o Do not put spaces before or after the equal sign ( =) when assigning
a value.
o Correct: name="John"
o Incorrect: name = "John"

3. Case Sensitivity:
o Variables are case-sensitive, so VAR and var are considered different
variables.
o Example: NAME="Alice" is not the same as name="Alice"
4. Defining Null Variables:

 You can create a variable without a value by using either of the following:

bash
Copy code
var=
var=""

5. Avoid Special Characters:

 Avoid using special characters like ?, *, and others in variable names as they may
have special meanings in the shell.

Example of a Shell Script:

Here's a simple shell script demonstrating user-defined variables, environment variables, and
positional variables:
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 not; if yes, then
-eq [ $a -eq $b ] is not true.
the condition becomes true.

Checks if the value of two operands are equal or not; if values are
-ne [ $a -ne $b ] is true.
not equal, then the condition becomes true.

Checks if the value of left operand is greater than the value of


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

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

Checks if the value of left operand is greater than or equal to the


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

Checks if the value of left operand is less than or equal to the


-le [ $a -le $b ] is true.
value of right operand; if yes, then the 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