0% found this document useful (0 votes)
13 views31 pages

Expt 8

The document provides an overview of shell programming in Linux, focusing on the kernel and shell components of the operating system. It details various types of shells, particularly the Bash shell, and explains shell scripting, including variable usage, arithmetic operations, and control structures. Additionally, it includes examples and exercises for practical application of shell scripting concepts.

Uploaded by

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

Expt 8

The document provides an overview of shell programming in Linux, focusing on the kernel and shell components of the operating system. It details various types of shells, particularly the Bash shell, and explains shell scripting, including variable usage, arithmetic operations, and control structures. Additionally, it includes examples and exercises for practical application of shell scripting concepts.

Uploaded by

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

Shell Programming in

Linux(bash)
OS kernal and Shell
• An operating system is made up of various components, but two core parts
are the kernel and the shell:
• Kernel: The kernel is the heart of the operating system. It is responsible for
managing hardware resources and providing a communication layer between
the hardware and the software. It operates in the background and handles
low-level tasks such as memory management, device control, and process
scheduling.
• Shell: The shell is the user-facing part of the operating system. It takes input
from the user in the form of commands, processes these commands, and
provides output. The shell can be accessed through a terminal, which serves
as the environment where commands are entered and results are displayed.
Types of Shells

Here are some of the most common types of shells in UNIX-like operating
systems:
1. Bash Shell (Bourne Again Shell): Bash is the most popular and widely used
shell in Linux distributions. It is an enhanced version of the original Bourne Shell
(sh) and adds many features like command-line editing, job control, and
scripting enhancements.
Key Features:
 Supports scripting and automation.
 Allows command history and tab completion.
 Provides useful built-in commands and functions.
 Default in most Linux distributions.
Types of Shells(Cont.)
C Shell (csh):C Shell is a shell that resembles the C programming
language in its syntax. It offers features like command history and job
control. It’s more suitable for users familiar with C programming.
Key Features:
• C-like syntax for scripting.
• Supports aliases and built-in commands.
• Allows job control and interactive use.
• Popular in older UNIX systems
Types of Shells(Cont.)
3. Corn Shell (ksh) Korn Shell is a shell that combines features of the
Bourne Shell (sh) and C Shell (csh). It provides powerful features for
both interactive and programming tasks. It’s known for its performance
and scripting capabilities.
Key Features:
• Combines features of sh and csh.
• Supports advanced scripting and functions.
• Provides job control and command history.
• Widely used in enterprise environments.
Why Bash Cell is common?
The Bash Shell is the most common shell because
• it is the default shell in many Linux distributions
• offers a rich set of feature
• is open-source, and is actively maintained.
• Its backward compatibility with older shells like the Bourne Shell
• ease of use
• cross-platform availability
• extensive community support
all contribute to its widespread adoption. Bash’s versatility in both interactive use
and scripting makes it the go-to choice for a wide range of users, from beginners to
advanced developers and system administrators
Shell Scripting
• Shell scripting is a powerful way to automate tasks in Linux. A shell
script is a file containing a series of commands that the shell executes
sequentially.

Make the Script Executable


1. Change the file's permissions to make it executable:
chmod +x script.sh
2. Run the script:
./script.sh
Shell Scripting using bash
Comments
Comments start with a # in bash scripting. This means that any line that
begins with a # is a comment and will be ignored by the interpreter.

# This is an example comment


# Both of these lines will be ignored by the interpreter
Variables and data types in
Bash
• Variables let you store data. You can use variables to
read, access, and manipulate data throughout your
script.
• There are no data types in Bash. In Bash, a variable is
capable of storing numeric values, individual
characters, or strings of characters.
• In Bash, you can use and set the variable values in the
following way
a=1
name=“John”
Variable naming conventions
In Bash scripting, the following are the variable naming conventions:
• Variable names should start with a letter or an underscore (_)
• Variable names can contain letters, numbers, and underscores (_).
• Variable names are case-sensitive.
• Variable names should not contain spaces or special characters.
• Use descriptive names that reflect the purpose of the variable.
• Avoid using reserved keywords, such as if, then, else, fi, and so on as
variable names.
Examples:
name
num1
_total
Arithmetic Operators

These operators are used to perform basic mathematical operations.


Addition: +
Subtraction: -
Multiplication: *
Division: /
Modulus (remainder): %
Exponentiation: **
Numeric Comparison operators
Equal to: -eq
Not equal to: -ne
Greater than: -gt
Less than: -lt
Greater than or equal to: -ge
Less than or equal to: -le
String Comparison
Equal to: =
Not equal to: !=
Less than (lexicographically): <
Greater than (lexicographically): >
Input and output in Bash scripts
How to read input?
We can read the user input using the read command.
For example
read name
How to display output?
we can use echo command Printing to the terminal:
Your first program
1.Create a bash script file
Open Text Editor to create a new program and save the file using .sh
extention
Or
you can use the command gedit program1.sh

The file content should be


echo “My First Script”

2. Run the code using the following command from Terminal

bash program1.sh
How to access the value of a
variable
$ is used to access the value of a variable
my_var="Hello"
echo $my_var # Output: Hello

a=2
b=3
c=$((a + b))
$(( ... )) syntax is used for performing arithmetic operations in Bash.
Make sure there’s a space around the + operator.
Program to read and print your
name
read name
echo “your name is $name”
Program to add to numbers
Conditional Statements in bash
following is the syntax for if statement
Example of if
if-Else Example
If-Elif-Else Example
Looping and Branching in Bash
While Statement
while [[condition]];
do
#Statement to be executed
done
Program to print numbers from 1-10
For statement varients..
2. for Loop with C-style Syntax
• Another way to write a for loop in Bash is using the C-style syntax,
where you explicitly define initialization, condition, and increment.
3. for Loop with a List of Values
case statement in Bash
• Here, "expression" is the value that we want to
compare, and "pattern1", "pattern2", "pattern3", and so
on are the patterns that we want to compare it against.
• The double semicolon ";;" separates each block of code
to execute for each pattern. The asterisk "*" represents
the default case, which executes if none of the specified
patterns match the expression.
Excercise

1. Write a bash script to read and print your address


2. Write a bash script to find the maximum of 3 numbers
3. Write a bash script to find the factorial of a number
4. Write a menu driven program to perform addition,subtraction
multiplication and division on two numbers

You might also like