SlideShare a Scribd company logo
1 of 24
Download to read offline
Dr. D. P.
Mishra
Digitally signed by Dr. D. P.
Mishra
DN: cn=Dr. D. P. Mishra, o=durg,
ou=BIT,
email=dpmishra@bitdurg.ac.in,
c=IN
Date: 2023.03.17 10:34:48 +05'30'
How to define User defined variables (UDV)
• Syntax: variable name=value
• NOTE: Here 'value' is assigned to given 'variablename'
For e.g.
$ no=10 # this is ok
$ 10=no # Error, NOT Ok
• Don't put spaces on either side of the equal sign
• $ no=10 is OK But here there will be problem for following
• $ no =10
• $ no= 10
• $ no = 10
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
More on Variables.
• Variables are case-sensitive, just like filename in Linux. For e.g.
$ no=10
$ No=11
$ NO=20
$ nO=2
• Above all are different variable name, so to print value 20 we have to
use $ echo $NO - and Not any of the following
$ echo $no # will print 10 but not 20
$ echo $No # will print 11 but not 20
$ echo $nO # will print 2 but not 20
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
More on variables ..
• How to Define variable x with value 10 and print it on screen ?
$ x=10
$ echo $x
How to Define variable xn with value Ram and print it on screen
$ xn=Ram
$ echo $xn
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
More on Variables ...
• How to print sum of two numbers, let's say 6 and 3
$ echo 6 + 3 This will print 6 + 3, not the sum 9
• For math operations in shell use expr, syntax is : expr op1 operator op2
• Where, op1 and op2 are any Integer Number (Number without decimal
point) and operator can be
+ Addition
- Subtraction
/ Division
% Modular, to find remainder
• For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2
• $ expr 6 + 3 Now It will print sum as 9 ,
• But $ expr 6+3 will not work because space is required between number
and operator
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
More on Variables….
• How to define two variable x=20, y=5 and then to print division of x
and y (i.e. x/y)
$x=20
$y=5
$ expr x / y
• Modify above and store division of x and y to variable called z
$ x=20
$ y=5
$ z=`expr x / y`
$ echo $z
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Shell Arithmetic
• Use to perform arithmetic operations For e.g.
$ expr 1 + 3
$ expr 2 - 1
$ expr 10 / 2
$ expr 20 % 3 # remainder read as 20 mod 3 and remainder is 2)
$ expr 10 * 3 # Multiplication use * not * since its wild card)
$ echo `expr 6 + 3` For the last statement not the following points
• First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ')
sign.
• Back quote is generally found on the key under tilde (~) on PC keyboards OR To the
above of TAB key.
• Second, expr is also end with ` i.e. back quote.
• Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum
• If you use double quote or single quote, it will NOT work,
• For eg. $ echo "expr 6 + 3" # It will print expr 6 + 3 $ echo 'expr 6 + 3'
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
if [condition]
then
statement1
else
statement2
fi
Command Description
&& Logical AND
-eq Equality check
-ne Inequality check
-lt Less Than
-le Less Than or Equal
-gt Greater Than
-ge Greater Than or Equal
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
If else fi statement
Switch Case Statement
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Syntax
case <word> in
<first pattern>)
<statements>
;;
<second pattern>)
<statements>
;;
*)
<default statements>
;;
esac
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example1
name="HowLinux"
case $name in
"Linux")
echo "Not quite right"
;;
"HowLinux")
echo "That's right!"
;;
esac
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example2
echo “Enter a number”
read num
case $num in
[0-9])
echo “you have entered a single digit number”
;;
[1-9][1-9])
echo “you have entered a two-digit number”
;;
[1-9][1-9][1-9])
echo “you have entered a three-digit number”
;;
*)
echo “your entry does not match any of the conditions”
;;
esac
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example3: case - esac
echo "Which color do you like best?"
echo "1 - Blue"
echo "2 - Red"
echo "3 - Yellow"
echo "4 - Green"
echo "5 - Orange"
read color;
case $color in
1) echo "Blue is a primary color.";;
2) echo "Red is a primary color.";;
3) echo "Yellow is a primary color.";;
4) echo "Green is a secondary color.";;
5) echo "Orange is a secondary color.";;
*) echo "This color is not available. Please choose a different one.";;
esac
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example4 : Script to print capital of state
entered by user
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
While Loop Syntax
while [ condition ]
do
command1
command2
done
• Here while, do, and done are built-in keywords.
• It will execute all the commands while the condition evaluates
to true.
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Nesting While loop
while command1 ; # this is loop1, the outer loop
do
Statement(s) to be executed if command1 is true
while command2 ; # this is loop2, the inner loop
do
Statement(s) to be executed if command2 is true
done
Statement(s) to be executed if command1 is true
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example1 : While loop to display numbers
from 1 to 10.
number = 1
while [ $number –lt 11 ]
do echo $number
((number++))
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example2
INPUT_STRING=hello
while [ "$INPUT_STRING" != "bye" ]
do
echo "Please type something in (bye to quit)"
read INPUT_STRING
echo "You typed: $INPUT_STRING"
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example3
a=0
while [ "$a" -lt 10 ] # this is loop1
do
b="$a"
while [ "$b" -ge 0 ] # this is loop2
do
echo -n "$b "
b=`expr $b - 1`
done
echo
a=`expr $a + 1`
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
#!/bin/sh
# This script finds the greatest common divisor (GCD) of two
numbers using the Euclidean algorithm
echo "Enter the first number: "
read num1
echo "Enter the second number: "
read num2
# Make sure num1 is greater than or equal to num2
if [ $num1 -lt $num2 ]
then
temp=$num1
num1=$num2
num2=$temp
fi
while [ $num2 -ne 0 ]
do
temp=$num2
num2=$((num1 % num2))
num1=$temp
done
echo "The greatest common divisor of the two numbers is
$num1."
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example4: Shell script for finding the greatest common
divisor (GCD) of two numbers using the Euclidean
algorithm:
For Loop
• For loop is another type of looping statement to execute a set of
commands for a certain number of times.
for var in list
do
command 1
command 2
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example1
for p_name in Ram Shyam Mahesh Rajesh
do
echo $p_name
done
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Example 2: Write a script to calculate factorial
of no
#!/bin/sh
# This script calculates the factorial of a given number
echo "Enter a number: "
read num
factorial=1
i=1
while [ $i -le $num ]
do
factorial=$((factorial * i))
i=$((i + 1))
done
echo "The factorial of $num is $factorial"
Linux
Laboratory
-
B.Tech.
6th
CSE
-
Dr.
D.
P.
Mishra,
BIT
Durg
Control flow and related shell cripts

More Related Content

Similar to Control flow and related shell cripts

C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
Srikanth
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
RedenOriola
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 

Similar to Control flow and related shell cripts (20)

python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
c++ Data Types and Selection
c++ Data Types and Selectionc++ Data Types and Selection
c++ Data Types and Selection
 
Improve Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptxImprove Your Edge on Machine Learning - Day 1.pptx
Improve Your Edge on Machine Learning - Day 1.pptx
 
C aptitude.2doc
C aptitude.2docC aptitude.2doc
C aptitude.2doc
 
Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01Captitude 2doc-100627004318-phpapp01
Captitude 2doc-100627004318-phpapp01
 
Python programing
Python programingPython programing
Python programing
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 
C# to python
C# to pythonC# to python
C# to python
 
901131 examples
901131 examples901131 examples
901131 examples
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Python
PythonPython
Python
 
Computer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .pptComputer 10 Quarter 3 Lesson .ppt
Computer 10 Quarter 3 Lesson .ppt
 
An Introduction : Python
An Introduction : PythonAn Introduction : Python
An Introduction : Python
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Basics of Python Programming
Basics of Python ProgrammingBasics of Python Programming
Basics of Python Programming
 
Prog1-L2.pptx
Prog1-L2.pptxProg1-L2.pptx
Prog1-L2.pptx
 

More from BIT DURG

Understanding WWW
Understanding WWWUnderstanding WWW
Understanding WWW
BIT DURG
 

More from BIT DURG (20)

HTML_DOM
HTML_DOMHTML_DOM
HTML_DOM
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Understanding WWW
Understanding WWWUnderstanding WWW
Understanding WWW
 
Computer Networks
Computer NetworksComputer Networks
Computer Networks
 
Computer Basics
Computer Basics Computer Basics
Computer Basics
 
ISDN & ATM
ISDN & ATMISDN & ATM
ISDN & ATM
 
Transport Control Protocol
Transport Control ProtocolTransport Control Protocol
Transport Control Protocol
 
Routing Protocols
Routing ProtocolsRouting Protocols
Routing Protocols
 
Internet Protocol.pdf
Internet Protocol.pdfInternet Protocol.pdf
Internet Protocol.pdf
 
Intternetworking With TCP/IP
Intternetworking With TCP/IPIntternetworking With TCP/IP
Intternetworking With TCP/IP
 
Computer Network Basics
Computer Network BasicsComputer Network Basics
Computer Network Basics
 
Types of Linux Shells
Types of Linux Shells Types of Linux Shells
Types of Linux Shells
 
File Access Permission
File Access PermissionFile Access Permission
File Access Permission
 
Filters & Vi Editor
Filters & Vi EditorFilters & Vi Editor
Filters & Vi Editor
 
Basic Linux Commands
Basic Linux CommandsBasic Linux Commands
Basic Linux Commands
 
Linux Installation
Linux InstallationLinux Installation
Linux Installation
 
Basics of GNU & Linux
Basics of GNU & LinuxBasics of GNU & Linux
Basics of GNU & Linux
 
National youth day
National youth dayNational youth day
National youth day
 
Visual Basic Tutorials
Visual Basic TutorialsVisual Basic Tutorials
Visual Basic Tutorials
 
Oss the freedom dpm 2018
Oss the freedom dpm 2018Oss the freedom dpm 2018
Oss the freedom dpm 2018
 

Recently uploaded

Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
jaanualu31
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 

Recently uploaded (20)

Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills KuwaitKuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
Kuwait City MTP kit ((+919101817206)) Buy Abortion Pills Kuwait
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 

Control flow and related shell cripts

  • 1. Dr. D. P. Mishra Digitally signed by Dr. D. P. Mishra DN: cn=Dr. D. P. Mishra, o=durg, ou=BIT, email=dpmishra@bitdurg.ac.in, c=IN Date: 2023.03.17 10:34:48 +05'30'
  • 2. How to define User defined variables (UDV) • Syntax: variable name=value • NOTE: Here 'value' is assigned to given 'variablename' For e.g. $ no=10 # this is ok $ 10=no # Error, NOT Ok • Don't put spaces on either side of the equal sign • $ no=10 is OK But here there will be problem for following • $ no =10 • $ no= 10 • $ no = 10 Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 3. More on Variables. • Variables are case-sensitive, just like filename in Linux. For e.g. $ no=10 $ No=11 $ NO=20 $ nO=2 • Above all are different variable name, so to print value 20 we have to use $ echo $NO - and Not any of the following $ echo $no # will print 10 but not 20 $ echo $No # will print 11 but not 20 $ echo $nO # will print 2 but not 20 Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 4. More on variables .. • How to Define variable x with value 10 and print it on screen ? $ x=10 $ echo $x How to Define variable xn with value Ram and print it on screen $ xn=Ram $ echo $xn Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 5. More on Variables ... • How to print sum of two numbers, let's say 6 and 3 $ echo 6 + 3 This will print 6 + 3, not the sum 9 • For math operations in shell use expr, syntax is : expr op1 operator op2 • Where, op1 and op2 are any Integer Number (Number without decimal point) and operator can be + Addition - Subtraction / Division % Modular, to find remainder • For e.g. 20 / 3 = 6 , to find remainder 20 % 3 = 2 • $ expr 6 + 3 Now It will print sum as 9 , • But $ expr 6+3 will not work because space is required between number and operator Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 6. More on Variables…. • How to define two variable x=20, y=5 and then to print division of x and y (i.e. x/y) $x=20 $y=5 $ expr x / y • Modify above and store division of x and y to variable called z $ x=20 $ y=5 $ z=`expr x / y` $ echo $z Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 7. Shell Arithmetic • Use to perform arithmetic operations For e.g. $ expr 1 + 3 $ expr 2 - 1 $ expr 10 / 2 $ expr 20 % 3 # remainder read as 20 mod 3 and remainder is 2) $ expr 10 * 3 # Multiplication use * not * since its wild card) $ echo `expr 6 + 3` For the last statement not the following points • First, before expr keyword we used ` (back quote) sign not the (single quote i.e. ') sign. • Back quote is generally found on the key under tilde (~) on PC keyboards OR To the above of TAB key. • Second, expr is also end with ` i.e. back quote. • Here expr 6 + 3 is evaluated to 9, then echo command prints 9 as sum • If you use double quote or single quote, it will NOT work, • For eg. $ echo "expr 6 + 3" # It will print expr 6 + 3 $ echo 'expr 6 + 3' Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 8. if [condition] then statement1 else statement2 fi Command Description && Logical AND -eq Equality check -ne Inequality check -lt Less Than -le Less Than or Equal -gt Greater Than -ge Greater Than or Equal Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg If else fi statement
  • 10. Syntax case <word> in <first pattern>) <statements> ;; <second pattern>) <statements> ;; *) <default statements> ;; esac Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 11. Example1 name="HowLinux" case $name in "Linux") echo "Not quite right" ;; "HowLinux") echo "That's right!" ;; esac Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 12. Example2 echo “Enter a number” read num case $num in [0-9]) echo “you have entered a single digit number” ;; [1-9][1-9]) echo “you have entered a two-digit number” ;; [1-9][1-9][1-9]) echo “you have entered a three-digit number” ;; *) echo “your entry does not match any of the conditions” ;; esac Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 13. Example3: case - esac echo "Which color do you like best?" echo "1 - Blue" echo "2 - Red" echo "3 - Yellow" echo "4 - Green" echo "5 - Orange" read color; case $color in 1) echo "Blue is a primary color.";; 2) echo "Red is a primary color.";; 3) echo "Yellow is a primary color.";; 4) echo "Green is a secondary color.";; 5) echo "Orange is a secondary color.";; *) echo "This color is not available. Please choose a different one.";; esac Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 14. Example4 : Script to print capital of state entered by user Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 15. While Loop Syntax while [ condition ] do command1 command2 done • Here while, do, and done are built-in keywords. • It will execute all the commands while the condition evaluates to true. Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 16. Nesting While loop while command1 ; # this is loop1, the outer loop do Statement(s) to be executed if command1 is true while command2 ; # this is loop2, the inner loop do Statement(s) to be executed if command2 is true done Statement(s) to be executed if command1 is true done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 17. Example1 : While loop to display numbers from 1 to 10. number = 1 while [ $number –lt 11 ] do echo $number ((number++)) done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 18. Example2 INPUT_STRING=hello while [ "$INPUT_STRING" != "bye" ] do echo "Please type something in (bye to quit)" read INPUT_STRING echo "You typed: $INPUT_STRING" done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 19. Example3 a=0 while [ "$a" -lt 10 ] # this is loop1 do b="$a" while [ "$b" -ge 0 ] # this is loop2 do echo -n "$b " b=`expr $b - 1` done echo a=`expr $a + 1` done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 20. #!/bin/sh # This script finds the greatest common divisor (GCD) of two numbers using the Euclidean algorithm echo "Enter the first number: " read num1 echo "Enter the second number: " read num2 # Make sure num1 is greater than or equal to num2 if [ $num1 -lt $num2 ] then temp=$num1 num1=$num2 num2=$temp fi while [ $num2 -ne 0 ] do temp=$num2 num2=$((num1 % num2)) num1=$temp done echo "The greatest common divisor of the two numbers is $num1." Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg Example4: Shell script for finding the greatest common divisor (GCD) of two numbers using the Euclidean algorithm:
  • 21. For Loop • For loop is another type of looping statement to execute a set of commands for a certain number of times. for var in list do command 1 command 2 done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 22. Example1 for p_name in Ram Shyam Mahesh Rajesh do echo $p_name done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 23. Example 2: Write a script to calculate factorial of no #!/bin/sh # This script calculates the factorial of a given number echo "Enter a number: " read num factorial=1 i=1 while [ $i -le $num ] do factorial=$((factorial * i)) i=$((i + 1)) done echo "The factorial of $num is $factorial" Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg