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

Control flow and related shell cripts

  • 1.
    Dr. D. P. Mishra Digitallysigned 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 defineUser 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 • Useto 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
  • 9.
  • 10.
    Syntax case <word> in <firstpattern>) <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 anumber” 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 : Scriptto 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 whilecommand1 ; # 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 : Whileloop 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 scriptfinds 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 • Forloop 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 inRam Shyam Mahesh Rajesh do echo $p_name done Linux Laboratory - B.Tech. 6th CSE - Dr. D. P. Mishra, BIT Durg
  • 23.
    Example 2: Writea 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