SlideShare a Scribd company logo
10
Most read
15
Most read
16
Most read
Unix Shell Scripts
-Prakash Lambha
Conditional Statements
 Unix Shell supports conditional statements
which are used to perform different actions
based on different conditions.
 Here we will explain following two decision making
statements:
1) The if...else statements
2) The case...esac statement
The if...else statements:
 If else statements are useful decision making statements
which can be used to select an option from a given set of
options.
 Unix Shell supports following forms of if..else
statements:
1.1) if...fi statement
1.2) if...else...fi statement
1.3) if...elif...else...fi statement
1.1 The if...fi statement
 The if...fi statement is the fundamental control
statement that allows Shell to make decisions and
execute statements conditionally.
 Syntax:
if [ expression ]
then
Statement(s) to be executed if expression is true
fi
1.1 The if...fi statement (Cont.)
 Here Shell expression is evaluated. If the resulting value
is true, given statement(s) are executed.
 If expression is false then no statement would be not
executed.
 Most of the times you will use comparison operators
while making decisions.
 Give you attention on the spaces between braces and
expression. This space is mandatory otherwise you
would get syntax error.
 If expression is a shell command then it would be
assumed true if it return 0 after its execution.
 If it is a Boolean expression then it would be true if it
returns true.
1.1 The if...fi statement (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
fi
if [ $a != $b ]
then
echo "a is not equal to b"
fi
This will produce following result:
1.2 The if...else...fi
 The next form of control statement that allows Shell
to execute statements in more controlled way and
making decision between two choices.
 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
1.2 The if...else...fi (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
This will produce following result:
a is not equal to b
1.3 The if...elif...fi
 is the one level advance form of control statement that allows Shell to make
correct decision out of several conditions.
Syntax:
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
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
1.3 The if...elif...fi (Cont.)
 Example:
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
elif [ $a -gt $b ]
then
echo "a is greater than b"
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
2. The case...esac
 You can use multiple if...elif statements to perform a
multi-way branch.
 However, this is not always the best solution,
especially when all of the branches depend on the
value of a single variable.
 Unix Shell supports case...esac statement which
handles exactly this situation, and it does so more
efficiently than repeated if...elif statements.
2. The case...esac (Cont.)
 Syntax:
case word in
pattern1)
Statement(s) to be executed if pattern1 matches
;;
pattern2)
Statement(s) to be executed if pattern2 matches
;;
pattern3)
Statement(s) to be executed if pattern3 matches
;;
esac
2. The case...esac (Cont.)
 Example:
#!/bin/sh
FRUIT="kiwi"
case "$FRUIT" in
"apple") echo "Apple pie is quite tasty."
;;
"banana") echo "I like banana nut bread."
;;
"kiwi") echo "New Zealand is famous for kiwi."
;;
esac
 This will produce following result:
New Zealand is famous for kiwi.
Loops: While Loop
 The while loop enables you to execute a set of
commands repeatedly until some condition occurs. It
is usually used when you need to manipulate the
value of a variable repeatedly.
 Syntax:
while command
do
Statement(s) to be executed if command is true
done
Loops: While Loop
 Example:
Here is a simple example that uses the while loop to
display the numbers zero to nine:
#!/bin/sh
a=0
while [ $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Loops: For Loop
 The for loop operate on lists of items. It repeats a set of
commands for every item in a list.
 Syntax:
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
 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.
Loops: For Loop
 Here is a simple example that uses for loop to span
through the given list of numbers:
#!/bin/sh
for var in 0 1 2 3 4 5 6 7 8 9
do
echo $var
done
Loops: Until Loop
 The while loop is perfect for a situation where you
need to execute a set of commands while some
condition is true. Sometimes you need to execute a
set of commands until a condition is true.
 Syntax:
until command
do
Statement(s) to be executed until command is true
done
Loops: Until Loop
 Here Shell command is evaluated.
 If the resulting value is false, given statement(s) are executed.
 If command is true then no statement would be not executed and program
would jump to the next line after done statement.
 Example:
Here is a simple example that uses the until loop to display the numbers
zero to nine:
#!/bin/sh
a=0
until [ ! $a -lt 10 ]
do
echo $a
a=`expr $a + 1`
done
Ad

Recommended

SHELL PROGRAMMING
SHELL PROGRAMMING
jinal thakrar
 
Shell scripting
Shell scripting
Manav Prasad
 
4. plsql
4. plsql
Amrit Kaur
 
Shell programming
Shell programming
Moayad Moawiah
 
Nested Queries Lecture
Nested Queries Lecture
Felipe Costa
 
Shell and its types in LINUX
Shell and its types in LINUX
SHUBHA CHATURVEDI
 
Sql commands
Sql commands
Pooja Dixit
 
Database Management - Lecture 2 - SQL select, insert, update and delete
Database Management - Lecture 2 - SQL select, insert, update and delete
Al-Mamun Sarkar
 
Database Design
Database Design
learnt
 
Code Optimization
Code Optimization
Akhil Kaushik
 
MYSQL.ppt
MYSQL.ppt
webhostingguy
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
Neeru Mittal
 
1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
Sql queries presentation
Sql queries presentation
NITISH KUMAR
 
SQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu
 
Stored procedure
Stored procedure
baabtra.com - No. 1 supplier of quality freshers
 
SQL Commands
SQL Commands
Sachidananda M H
 
SQL Queries
SQL Queries
Nilt1234
 
SQL JOIN
SQL JOIN
Ritwik Das
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
SQL DDL
SQL DDL
Vikas Gupta
 
Relational model
Relational model
Dabbal Singh Mahara
 
Constraints In Sql
Constraints In Sql
Anurag
 
Unix Operating System
Unix Operating System
MahakKasliwal
 
Sql ppt
Sql ppt
Anuja Lad
 
Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Cursors
Cursors
Priyanka Yadav
 
Product Catalog Powerpoint Presentation Template
Product Catalog Powerpoint Presentation Template
adriandragne
 

More Related Content

What's hot (20)

Database Design
Database Design
learnt
 
Code Optimization
Code Optimization
Akhil Kaushik
 
MYSQL.ppt
MYSQL.ppt
webhostingguy
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
Neeru Mittal
 
1. Introduction to DBMS
1. Introduction to DBMS
koolkampus
 
Sql queries presentation
Sql queries presentation
NITISH KUMAR
 
SQL - DML and DDL Commands
SQL - DML and DDL Commands
Shrija Madhu
 
Stored procedure
Stored procedure
baabtra.com - No. 1 supplier of quality freshers
 
SQL Commands
SQL Commands
Sachidananda M H
 
SQL Queries
SQL Queries
Nilt1234
 
SQL JOIN
SQL JOIN
Ritwik Das
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
SQL DDL
SQL DDL
Vikas Gupta
 
Relational model
Relational model
Dabbal Singh Mahara
 
Constraints In Sql
Constraints In Sql
Anurag
 
Unix Operating System
Unix Operating System
MahakKasliwal
 
Sql ppt
Sql ppt
Anuja Lad
 
Object oriented programming c++
Object oriented programming c++
Ankur Pandey
 
Cursors
Cursors
Priyanka Yadav
 

Viewers also liked (20)

Product Catalog Powerpoint Presentation Template
Product Catalog Powerpoint Presentation Template
adriandragne
 
Linux shell script-1
Linux shell script-1
兎 伊藤
 
Shell Script Tutorial
Shell Script Tutorial
Quang Minh Đoàn
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
BITS
 
Shell Scripting
Shell Scripting
Gaurav Shinde
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
003 scripting
003 scripting
Sherif Mousa
 
Barack obama christian or heathen (slovak)
Barack obama christian or heathen (slovak)
VogelDenise
 
Barack obama christian or heathen (chinese - traditional)
Barack obama christian or heathen (chinese - traditional)
VogelDenise
 
SYRIA CRISIS - (USA) CHEMICAL WEAPONS ATTACK (slovak)
SYRIA CRISIS - (USA) CHEMICAL WEAPONS ATTACK (slovak)
VogelDenise
 
Barack obama christian or heathen (polish)
Barack obama christian or heathen (polish)
VogelDenise
 
Intelligence
Intelligence
mhelton3
 
Barack obama christian or heathen (catalan)
Barack obama christian or heathen (catalan)
VogelDenise
 
092712 julian assange (president obama's audacity) - finnish
092712 julian assange (president obama's audacity) - finnish
VogelDenise
 
United States of America – IMMIGRATION REFORM - GEORGIAN
United States of America – IMMIGRATION REFORM - GEORGIAN
VogelDenise
 
Barack obama christian or heathen (bulgarian)
Barack obama christian or heathen (bulgarian)
VogelDenise
 
060812 EEOC Response(CHINESE - Traditional)
060812 EEOC Response(CHINESE - Traditional)
VogelDenise
 
Product Catalog Powerpoint Presentation Template
Product Catalog Powerpoint Presentation Template
adriandragne
 
Linux shell script-1
Linux shell script-1
兎 伊藤
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
BITS
 
Unix Shell Scripting Basics
Unix Shell Scripting Basics
Dr.Ravi
 
Barack obama christian or heathen (slovak)
Barack obama christian or heathen (slovak)
VogelDenise
 
Barack obama christian or heathen (chinese - traditional)
Barack obama christian or heathen (chinese - traditional)
VogelDenise
 
SYRIA CRISIS - (USA) CHEMICAL WEAPONS ATTACK (slovak)
SYRIA CRISIS - (USA) CHEMICAL WEAPONS ATTACK (slovak)
VogelDenise
 
Barack obama christian or heathen (polish)
Barack obama christian or heathen (polish)
VogelDenise
 
Intelligence
Intelligence
mhelton3
 
Barack obama christian or heathen (catalan)
Barack obama christian or heathen (catalan)
VogelDenise
 
092712 julian assange (president obama's audacity) - finnish
092712 julian assange (president obama's audacity) - finnish
VogelDenise
 
United States of America – IMMIGRATION REFORM - GEORGIAN
United States of America – IMMIGRATION REFORM - GEORGIAN
VogelDenise
 
Barack obama christian or heathen (bulgarian)
Barack obama christian or heathen (bulgarian)
VogelDenise
 
060812 EEOC Response(CHINESE - Traditional)
060812 EEOC Response(CHINESE - Traditional)
VogelDenise
 
Ad

Similar to Unix shell scripts (20)

Licão 11 decision making - statement
Licão 11 decision making - statement
Acácio Oliveira
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
Vpmv
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
Nihar Ranjan Paital
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 
Bash Programming
Bash Programming
Kiplangat Chelule
 
What is a shell script
What is a shell script
Dr.M.Karthika parthasarathy
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
 
First steps in C-Shell
First steps in C-Shell
Brahma Killampalli
 
Shell Scripting Structured Commands
Shell Scripting Structured Commands
Don Bosco BSIT
 
Module 03 Programming on Linux
Module 03 Programming on Linux
Tushar B Kute
 
Scripting 101
Scripting 101
ohardebol
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
OS.pdf
OS.pdf
ErPawanKumar3
 
Osp2.pdf
Osp2.pdf
ErPawanKumar3
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Zyxware Technologies
 
Licão 11 decision making - statement
Licão 11 decision making - statement
Acácio Oliveira
 
Case, Loop & Command line args un Unix
Case, Loop & Command line args un Unix
Vpmv
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
Nihar Ranjan Paital
 
Shell Programming Language in Operating System .pptx
Shell Programming Language in Operating System .pptx
SherinRappai
 
Unix Shell Programming subject shell scripting ppt
Unix Shell Programming subject shell scripting ppt
Radhika Ajadka
 
34-shell-programming asda asda asd asd.ppt
34-shell-programming asda asda asd asd.ppt
poyotero
 
34-shell-programming.ppt
34-shell-programming.ppt
KiranMantri
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
AOS_Module_3ssssssssssssssssssssssssssss.pptx
AOS_Module_3ssssssssssssssssssssssssssss.pptx
rapiwip803
 
Advanced linux chapter ix-shell script
Advanced linux chapter ix-shell script
Eliezer Moraes
 
Shell Scripting Structured Commands
Shell Scripting Structured Commands
Don Bosco BSIT
 
Module 03 Programming on Linux
Module 03 Programming on Linux
Tushar B Kute
 
Scripting 101
Scripting 101
ohardebol
 
ShellProgramming and Script in operating system
ShellProgramming and Script in operating system
vinitasharma749430
 
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Introduction to Bash Scripting, Zyxware Technologies, CSI Students Convention...
Zyxware Technologies
 
Ad

Recently uploaded (20)

Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
The Future of AI Agent Development Trends to Watch.pptx
The Future of AI Agent Development Trends to Watch.pptx
Lisa ward
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Oh, the Possibilities - Balancing Innovation and Risk with Generative AI.pdf
Priyanka Aash
 
Security Tips for Enterprise Azure Solutions
Security Tips for Enterprise Azure Solutions
Michele Leroux Bustamante
 
Securing Account Lifecycles in the Age of Deepfakes.pptx
Securing Account Lifecycles in the Age of Deepfakes.pptx
FIDO Alliance
 
You are not excused! How to avoid security blind spots on the way to production
You are not excused! How to avoid security blind spots on the way to production
Michele Leroux Bustamante
 
Techniques for Automatic Device Identification and Network Assignment.pdf
Techniques for Automatic Device Identification and Network Assignment.pdf
Priyanka Aash
 
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Enhance GitHub Copilot using MCP - Enterprise version.pdf
Nilesh Gule
 
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
War_And_Cyber_3_Years_Of_Struggle_And_Lessons_For_Global_Security.pdf
biswajitbanerjee38
 
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik - Passionate Tech Enthusiast
Raman Bhaumik
 
The Future of AI Agent Development Trends to Watch.pptx
The Future of AI Agent Development Trends to Watch.pptx
Lisa ward
 
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Coordinated Disclosure for ML - What's Different and What's the Same.pdf
Priyanka Aash
 
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
A Constitutional Quagmire - Ethical Minefields of AI, Cyber, and Privacy.pdf
Priyanka Aash
 
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Powering Multi-Page Web Applications Using Flow Apps and FME Data Streaming
Safe Software
 
cnc-processing-centers-centateq-p-110-en.pdf
cnc-processing-centers-centateq-p-110-en.pdf
AmirStern2
 
"Database isolation: how we deal with hundreds of direct connections to the d...
"Database isolation: how we deal with hundreds of direct connections to the d...
Fwdays
 
AI VIDEO MAGAZINE - June 2025 - r/aivideo
AI VIDEO MAGAZINE - June 2025 - r/aivideo
1pcity Studios, Inc
 
OpenPOWER Foundation & Open-Source Core Innovations
OpenPOWER Foundation & Open-Source Core Innovations
IBM
 
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
“Key Requirements to Successfully Implement Generative AI in Edge Devices—Opt...
Edge AI and Vision Alliance
 
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Smarter Aviation Data Management: Lessons from Swedavia Airports and Sweco
Safe Software
 
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Creating Inclusive Digital Learning with AI: A Smarter, Fairer Future
Impelsys Inc.
 
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance Seminar State of Passkeys.pptx
FIDO Alliance
 

Unix shell scripts

  • 2. Conditional Statements  Unix Shell supports conditional statements which are used to perform different actions based on different conditions.  Here we will explain following two decision making statements: 1) The if...else statements 2) The case...esac statement
  • 3. The if...else statements:  If else statements are useful decision making statements which can be used to select an option from a given set of options.  Unix Shell supports following forms of if..else statements: 1.1) if...fi statement 1.2) if...else...fi statement 1.3) if...elif...else...fi statement
  • 4. 1.1 The if...fi statement  The if...fi statement is the fundamental control statement that allows Shell to make decisions and execute statements conditionally.  Syntax: if [ expression ] then Statement(s) to be executed if expression is true fi
  • 5. 1.1 The if...fi statement (Cont.)  Here Shell expression is evaluated. If the resulting value is true, given statement(s) are executed.  If expression is false then no statement would be not executed.  Most of the times you will use comparison operators while making decisions.  Give you attention on the spaces between braces and expression. This space is mandatory otherwise you would get syntax error.  If expression is a shell command then it would be assumed true if it return 0 after its execution.  If it is a Boolean expression then it would be true if it returns true.
  • 6. 1.1 The if...fi statement (Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" fi if [ $a != $b ] then echo "a is not equal to b" fi This will produce following result:
  • 7. 1.2 The if...else...fi  The next form of control statement that allows Shell to execute statements in more controlled way and making decision between two choices.  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
  • 8. 1.2 The if...else...fi (Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" else echo "a is not equal to b" fi This will produce following result: a is not equal to b
  • 9. 1.3 The if...elif...fi  is the one level advance form of control statement that allows Shell to make correct decision out of several conditions. Syntax: if [ expression 1 ] then Statement(s) to be executed if expression 1 is true elif [ expression 2 ] 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
  • 10. 1.3 The if...elif...fi (Cont.)  Example: #!/bin/sh a=10 b=20 if [ $a == $b ] then echo "a is equal to b" elif [ $a -gt $b ] then echo "a is greater than b" elif [ $a -lt $b ] then echo "a is less than b" else echo "None of the condition met" fi
  • 11. 2. The case...esac  You can use multiple if...elif statements to perform a multi-way branch.  However, this is not always the best solution, especially when all of the branches depend on the value of a single variable.  Unix Shell supports case...esac statement which handles exactly this situation, and it does so more efficiently than repeated if...elif statements.
  • 12. 2. The case...esac (Cont.)  Syntax: case word in pattern1) Statement(s) to be executed if pattern1 matches ;; pattern2) Statement(s) to be executed if pattern2 matches ;; pattern3) Statement(s) to be executed if pattern3 matches ;; esac
  • 13. 2. The case...esac (Cont.)  Example: #!/bin/sh FRUIT="kiwi" case "$FRUIT" in "apple") echo "Apple pie is quite tasty." ;; "banana") echo "I like banana nut bread." ;; "kiwi") echo "New Zealand is famous for kiwi." ;; esac  This will produce following result: New Zealand is famous for kiwi.
  • 14. Loops: While Loop  The while loop enables you to execute a set of commands repeatedly until some condition occurs. It is usually used when you need to manipulate the value of a variable repeatedly.  Syntax: while command do Statement(s) to be executed if command is true done
  • 15. Loops: While Loop  Example: Here is a simple example that uses the while loop to display the numbers zero to nine: #!/bin/sh a=0 while [ $a -lt 10 ] do echo $a a=`expr $a + 1` done
  • 16. Loops: For Loop  The for loop operate on lists of items. It repeats a set of commands for every item in a list.  Syntax: for var in word1 word2 ... wordN do Statement(s) to be executed for every word. done  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.
  • 17. Loops: For Loop  Here is a simple example that uses for loop to span through the given list of numbers: #!/bin/sh for var in 0 1 2 3 4 5 6 7 8 9 do echo $var done
  • 18. Loops: Until Loop  The while loop is perfect for a situation where you need to execute a set of commands while some condition is true. Sometimes you need to execute a set of commands until a condition is true.  Syntax: until command do Statement(s) to be executed until command is true done
  • 19. Loops: Until Loop  Here Shell command is evaluated.  If the resulting value is false, given statement(s) are executed.  If command is true then no statement would be not executed and program would jump to the next line after done statement.  Example: Here is a simple example that uses the until loop to display the numbers zero to nine: #!/bin/sh a=0 until [ ! $a -lt 10 ] do echo $a a=`expr $a + 1` done

Editor's Notes