Expt 8
Expt 8
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.
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