Los MP
Los MP
Our Micro project is based of Shell Programming in Linux Operating system. Linux is an open
source operating system (OS). An operating system is the software that directly manages a system’s
hardware and resources, like CPU, memory, and storage. The OS sits between applications and hardware
and makes the connections between all of your software and the physical resources that do the work.
We saw that shells are interactive, they accept commands as input from users and execute them. But if we
want to execute a bunch of commands again and again (like a loop), we have to type in all commands each
time in the terminal.
1
1.Rationale :
Operating System is the interface between the user and the computer system. Nowadays LINUX is
one of the most widely used operating system. Knowledge of LINUX operating system is essential
as it provides many features such as multitasking, multiuser, security etc. which are mainly used in
both server and workstation systems. So, this course will enable the students to inculcate the basics
of LINUX Operating System, writing Shell scripts as well as administer the network.
3. Course Outcomes :
1)Use Basic Linux commands and utilities.
2) Develop Shell program for solving different problems.
2
Action Plan:
3
Introduction
Usually, shells are interactive, which means they accept commands as input from users and
execute them. However, sometimes we want to execute a bunch of commands routinely, so we
have to type in all commands each time in the terminal.
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. Shell scripts are similar to the batch file in MS-DOS. Each shell
script is saved with `.sh` file extension e.g., myscript.sh.
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.
Types of Shell
There are two main shells in Linux:
1. The Bourne Shell: The prompt for this shell is $ and its derivatives are listed below:
2. The C shell: The prompt for this shell is %, and its subcategories are:
4
Concepts :
Why do we need shell scripts?
• The command and syntax are exactly the same as those directly entered in the
command line, so programmers do not need to switch to entirely different
syntax
• Writing shell scripts are much quicker
• Quick start
• Interactive debugging etc.
•
Some Disadvantages of shell scripts
• Prone to costly errors, a single mistake can change the command which might
be harmful.
• Slow execution speed
• Design flaws within the language syntax or implementation
• Not well suited for large and complex task
• Provide minimal data structure unlike other scripting languages. etc.
As discussed earlier, Variables store data in the form of characters and numbers. Similarly, Shell
variables are used to store information and they can by the shell only.
5
Conditional Statements :
There are total 5 conditional statements which can be used in bash programming
1. if statement
2. if-else statement
3. if..elif..else..fi statement (Else If ladder)
4. if..then..else..if..then..fi..fi..(Nested if)
5. switch statement
1.if statement
Syntax:
if [ expression ]
then
statement
fi
Example :
6
2.if-else statement
If specified condition is not true in if part then else part will be execute.
Syntax
if [ expression ]
then
statement1
else
statement2
fi
Example :
7
3.if..elif..else..fi statement (Else If ladder)
To use multiple conditions in one if-else block, then elif keyword is used in shell. If
expression1 is true then it executes statement 1 and 2, and this process continues. If
none of the condition is true then it processes else part.
Syntax
if [ expression1 ]
then
statement1
statement2
elif [ expression2 ]
then
statement3
statement4
else
statement5
fi
Example :
8
4.if..then..else..if..then..fi..fi..(Nested if)
Nested if-else block can be used when, one condition is satisfies then it again checks
another condition. In the syntax, if expression1 is false then it processes else part, and
again expression2 will be check.
Syntax:
if [ expression1 ]
then
statement1
statement2
else
if [ expression2 ]
then
statement3
fi
fi
Example :
9
5.switch statement
case statement works as a switch statement if specified value match with the pattern
then it will execute a block of that particular pattern
When a match is found all of the associated statements until the double semicolon (;;) is
executed.
A case will be terminated when the last command is executed.
If there is no match, the exit status of the case is zero.
Syntax:
case in
Pattern 1) Statement 1;;
Pattern n) Statement n;;
esac
Example :
10
Looping Statements :
There are total 3 looping statements that can be used in bash programming
1. while statement
2. for statement
3. until statement
1.while Statement :
Here the command is evaluated and based on the resulting loop will execute, if the command is raise
to false then the loop will be terminated.
Synatx :
while <condition>
do
<command 1>
<command 2>
<etc>
done
Example :
11
2.for Statement :
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces
(words). Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word1 to wordN.
Synatx :
for <var> in <value1 value2 ... valuen>
do
<command 1>
<command 2>
<etc>
done
Example :
12
3.until Statement :
The until loop is executed as many times as the condition/command evaluates too false. The loop
terminates when the condition/command becomes true.
Synatx :
until <condition>
do
<command 1>
<command 2>
<etc>
done
Example :
13
Skills Developed :
Conclusion:
In our project Shell Programming in Linux we learn to write a series of commands for the shell to
execute. It can combine lengthy and repetitive sequences of commands into a single and simple
script that can be stored and executed anytime which, reduces programming efforts. Kernel is the
nucleus of the operating systems, and it communicates between hardware and software.Shell is a
program which interprets user commands through CLI like Terminal.The Bourne shell and the C
shell are the most used shells in Linux.Linux Shell scripting is writing a series of command for the
shell to execute.Shell variables store the value of a string or a number for the shell to read.Shell
scripting in Linux can help you create complex programs containing conditional statements, loops,
and functions.
Reference :
https://www.geeksforgeeks.org/introduction-linux-shell-shell-scripting/
https://www.onlinegdb.com/online_bash_shell
https://www.tutorialspoint.com/unix/unix-shell-loops.htm
14