SlideShare a Scribd company logo
1 of 30
Presentation on
 Looping Conditions and
Command Line Arguments
Conditional loop
• Conditional loop is a way for computer
  programs to repeat one or more various
  steps depending on conditions set either
  by the programmer initially or real-time
  by the actual program.
• Basically a loop has a conditional
  statement or a command and body of that
  loop which has some list of commands or
  statements to be executed repeatedly.
While Loop
• The while loop is used to execute a set of
  commands repeatedly until some
  condition occurs.
• It is usually used when the value of a
  variable has to be manipulated repeatedly.
Types
There are 2 types of while loop. They are,

Finite while loop : it is the one in which
 the loop or command will be executed
 finite times.
Infinite while loop : it is the one in which
 the set of commands will be executed
 infinite times.
Basic syntax

While command
do
    list
done

It can also be written as,

While command ; do list ; done
Steps to execute a while loop
1. Execute command.
2. If the exit status of command is
   nonzero, exit from the while loop
3. If the exit status of command is
   zero, execute list.
4. When list finishes execution, return to
   step 1.
For example,
                              The output looks like,
x=0                           0
while [ $x –lt 10 ]           1
do                            2
    echo $x                   3
     x=`echo “$x + 1” | bc`   4
done                          5
                              6
                              7
                              8
                              9
Example(2)
c=1                     Output,
while [ $c -le 5 ]      Welcome 1 times
 do                     Welcome 2 times
    echo "Welcome $c    Welcome 3 times
  times"                Welcome 4 times
    (( c++ ))           Welcome 5 times
 done
Infinite while loop
 It is a type of while loop which repeats its
 output infinite times.
 We can use some commands to
 accomplish infinite loop. They are,
        true
        false
        :       etc…
Example for infinite while loop

while :
 do
    echo “jhgiwu“
 done

It displays the output repeatedly.
To stop that, we have to press ctrl+c
Nested while loop

• It is a loop in which a while loop will be a
  part of the body of another while loop.
• There is no restrictions for the number of
  nested while loops.
• But it will b better to avoid more than 5
  nested loops.
Syntax is,
while command1 ; # this is loop1, the outer loop
do
    list1
    while command2 ; # this is loop2, the inner
  loop
      do
          list2
      done
      list3
done
Example,

x=0
while [ "$x" -lt 10 ] ; # this is loop1
 do
    y="$x"
    while [ "$y" -ge 0 ] ; # this is loop2
     do
         echo "$y c"
         y=´echo "$y - 1" | bc´
     done
     echo
     x=´echo "$x + 1" | bc´
 done
The output will be,

0
10
210
3210
43210
543210
6543210
76543210
876543210
9876543210
For loop


• The for loop is used to execute a set of
  commands repeatedly for each item in a
  list.
• One of its most common uses is in
  performing the same set of commands for
  a large number of files.
The common syntax is,
for name in word1 word2 ... wordN
  do
     list
  done

It can also be written as,

for name in word1 word2 ... wordN ; do list ; done
For a simple for loop,

for i in 0 1 2 3 4 5 6 7 8 9   The output is,
 do                            0
    echo $i                    1
 done                          2
                               3
                               4
                               5
                               6
                               7
                               8
                               9
another example,

for i in `cat 1.txt`     Output is,
 do
     echo $i             F
  done                   S
                         T
The contents in 1.txt,
F
S
T
Example(3)
alpha="a b c d e"             Output,
count=0                       a
for l in $alpha
                              b
 do
  count=`expr $count + 1`     c
  echo $l                     d
 done                         e
Example

for color in red green pink white black
 do
echo $color
done
echo “Done!!!”
Infinite for loop

for (( ; ; ))
do
echo “welcome"
sleep 1
done
Command line arguments
• The arguments used to pass to the shell
  scripts while interpreting are called
  command line arguments.
• These arguments can be passed using
  some positional parameters i.e, $1, $2, ect.
• There are 9 general positional parameters.
  If we have to give 10th or more than 10
  args we have to use {}. i.e. ${10}.
• Each parameter corresponds to position of
  the arguments on the command line.
Command line arguments

•   $0 indicates the name of the script.
•   $1 indicates the 1st argument of that script.
•   $2 indicates the 2nd argument.
•   $$ used to denote the process ID.
•   $# used to count the number of arguments.
•   $* denotes all arguments.
A simple example,

var1=$1
var2=$2
var3=` expr $var1 + $var2 `
echo $var3

 chmod +x file.sh
./abcd.sh 2 3

Output,
5
In the previous example,
If we do not give command
line arguments,




If we give more than 2
arguments,
Using argument status,
var1=$1               chmod +x file1.sh
var2=$2              ./file1.sh 2 3
var3=`expr $var1 +    output,
  $var2`             5
if [$# -ne 2 ]
then                 ./file1.sh 2 3 4
echo “no”            Output,
exit 1               no
else
echo $var3
fi
Any Queries????
Thank You 

More Related Content

What's hot

UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsNihar Ranjan Paital
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell ScriptingRaghu nath
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningASIT Education
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2Nihar Ranjan Paital
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programmingkayalkarnan
 
What's New in PHP 5.5
What's New in PHP 5.5What's New in PHP 5.5
What's New in PHP 5.5Corey Ballou
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Kang-min Liu
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest UpdatesIftekhar Eather
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Lin Yo-An
 
Introduction to go
Introduction to goIntroduction to go
Introduction to goJaehue Jang
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlHideaki Ohno
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMRaveen Perera
 
Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019Michał Kurzeja
 

What's hot (20)

UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Defer, Panic, Recover
Defer, Panic, RecoverDefer, Panic, Recover
Defer, Panic, Recover
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
32 shell-programming
32 shell-programming32 shell-programming
32 shell-programming
 
What's New in PHP 5.5
What's New in PHP 5.5What's New in PHP 5.5
What's New in PHP 5.5
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)Good Evils In Perl (Yapc Asia)
Good Evils In Perl (Yapc Asia)
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
Faster Python, FOSDEM
Faster Python, FOSDEMFaster Python, FOSDEM
Faster Python, FOSDEM
 
C++ control loops
C++ control loopsC++ control loops
C++ control loops
 
Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015Code Generation in PHP - PHPConf 2015
Code Generation in PHP - PHPConf 2015
 
Perl Intro 4 Debugger
Perl Intro 4 DebuggerPerl Intro 4 Debugger
Perl Intro 4 Debugger
 
Introduction to go
Introduction to goIntroduction to go
Introduction to go
 
The new features of PHP 7
The new features of PHP 7The new features of PHP 7
The new features of PHP 7
 
Yapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed PerlYapcasia2011 - Hello Embed Perl
Yapcasia2011 - Hello Embed Perl
 
Coding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBMCoding in GO - GDG SL - NSBM
Coding in GO - GDG SL - NSBM
 
Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019
 

Viewers also liked

Viewers also liked (14)

6 sosialisasi
6 sosialisasi6 sosialisasi
6 sosialisasi
 
บทที่1
บทที่1บทที่1
บทที่1
 
Basic commands
Basic commandsBasic commands
Basic commands
 
2008,2009,2010
2008,2009,20102008,2009,2010
2008,2009,2010
 
Matt's Memoir
Matt's MemoirMatt's Memoir
Matt's Memoir
 
Sosiologi
SosiologiSosiologi
Sosiologi
 
Акцизне оподаткування тютюнових виробів в Україні: міфи і факти
Акцизне оподаткування тютюнових виробів в Україні: міфи і фактиАкцизне оподаткування тютюнових виробів в Україні: міфи і факти
Акцизне оподаткування тютюнових виробів в Україні: міфи і факти
 
Hiv and aids
Hiv and aidsHiv and aids
Hiv and aids
 
España
EspañaEspaña
España
 
Plan ahead
Plan aheadPlan ahead
Plan ahead
 
My works
My worksMy works
My works
 
UNL presentation May 2016
UNL presentation May 2016UNL presentation May 2016
UNL presentation May 2016
 
Poultry Production
Poultry ProductionPoultry Production
Poultry Production
 
Evian
EvianEvian
Evian
 

Similar to Scripting ppt (20)

OS.pdf
OS.pdfOS.pdf
OS.pdf
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Syntax
SyntaxSyntax
Syntax
 
Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 
Shell programming
Shell programmingShell programming
Shell programming
 
Iteration
IterationIteration
Iteration
 
34-shell-programming.ppt
34-shell-programming.ppt34-shell-programming.ppt
34-shell-programming.ppt
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Licão 12 decision loops - statement iteration
Licão 12 decision loops - statement iterationLicão 12 decision loops - statement iteration
Licão 12 decision loops - statement iteration
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Osp2.pdf
Osp2.pdfOsp2.pdf
Osp2.pdf
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
shellScriptAlt.pptx
shellScriptAlt.pptxshellScriptAlt.pptx
shellScriptAlt.pptx
 
Advanced Scripting - 2 (Ch-8)
Advanced Scripting - 2 (Ch-8)Advanced Scripting - 2 (Ch-8)
Advanced Scripting - 2 (Ch-8)
 
Loops in Python.pptx
Loops in Python.pptxLoops in Python.pptx
Loops in Python.pptx
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Shell scripting
Shell scriptingShell scripting
Shell scripting
 
Unix
UnixUnix
Unix
 
Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands Course 102: Lecture 8: Composite Commands
Course 102: Lecture 8: Composite Commands
 
Advanced perl finer points ,pack&unpack,eval,files
Advanced perl   finer points ,pack&unpack,eval,filesAdvanced perl   finer points ,pack&unpack,eval,files
Advanced perl finer points ,pack&unpack,eval,files
 

Recently uploaded

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxdhanalakshmis0310
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
 

Recently uploaded (20)

Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Scripting ppt

  • 1. Presentation on Looping Conditions and Command Line Arguments
  • 2. Conditional loop • Conditional loop is a way for computer programs to repeat one or more various steps depending on conditions set either by the programmer initially or real-time by the actual program. • Basically a loop has a conditional statement or a command and body of that loop which has some list of commands or statements to be executed repeatedly.
  • 3. While Loop • The while loop is used to execute a set of commands repeatedly until some condition occurs. • It is usually used when the value of a variable has to be manipulated repeatedly.
  • 4. Types There are 2 types of while loop. They are, Finite while loop : it is the one in which the loop or command will be executed finite times. Infinite while loop : it is the one in which the set of commands will be executed infinite times.
  • 5. Basic syntax While command do list done It can also be written as, While command ; do list ; done
  • 6. Steps to execute a while loop 1. Execute command. 2. If the exit status of command is nonzero, exit from the while loop 3. If the exit status of command is zero, execute list. 4. When list finishes execution, return to step 1.
  • 7. For example, The output looks like, x=0 0 while [ $x –lt 10 ] 1 do 2 echo $x 3 x=`echo “$x + 1” | bc` 4 done 5 6 7 8 9
  • 8. Example(2) c=1 Output, while [ $c -le 5 ] Welcome 1 times do Welcome 2 times echo "Welcome $c Welcome 3 times times" Welcome 4 times (( c++ )) Welcome 5 times done
  • 9. Infinite while loop  It is a type of while loop which repeats its output infinite times.  We can use some commands to accomplish infinite loop. They are, true false : etc…
  • 10. Example for infinite while loop while : do echo “jhgiwu“ done It displays the output repeatedly.
  • 11.
  • 12. To stop that, we have to press ctrl+c
  • 13. Nested while loop • It is a loop in which a while loop will be a part of the body of another while loop. • There is no restrictions for the number of nested while loops. • But it will b better to avoid more than 5 nested loops.
  • 14. Syntax is, while command1 ; # this is loop1, the outer loop do list1 while command2 ; # this is loop2, the inner loop do list2 done list3 done
  • 15. Example, x=0 while [ "$x" -lt 10 ] ; # this is loop1 do y="$x" while [ "$y" -ge 0 ] ; # this is loop2 do echo "$y c" y=´echo "$y - 1" | bc´ done echo x=´echo "$x + 1" | bc´ done
  • 16. The output will be, 0 10 210 3210 43210 543210 6543210 76543210 876543210 9876543210
  • 17. For loop • The for loop is used to execute a set of commands repeatedly for each item in a list. • One of its most common uses is in performing the same set of commands for a large number of files.
  • 18. The common syntax is, for name in word1 word2 ... wordN do list done It can also be written as, for name in word1 word2 ... wordN ; do list ; done
  • 19. For a simple for loop, for i in 0 1 2 3 4 5 6 7 8 9 The output is, do 0 echo $i 1 done 2 3 4 5 6 7 8 9
  • 20. another example, for i in `cat 1.txt` Output is, do echo $i F done S T The contents in 1.txt, F S T
  • 21. Example(3) alpha="a b c d e" Output, count=0 a for l in $alpha b do count=`expr $count + 1` c echo $l d done e
  • 22. Example for color in red green pink white black do echo $color done echo “Done!!!”
  • 23. Infinite for loop for (( ; ; )) do echo “welcome" sleep 1 done
  • 24. Command line arguments • The arguments used to pass to the shell scripts while interpreting are called command line arguments. • These arguments can be passed using some positional parameters i.e, $1, $2, ect. • There are 9 general positional parameters. If we have to give 10th or more than 10 args we have to use {}. i.e. ${10}. • Each parameter corresponds to position of the arguments on the command line.
  • 25. Command line arguments • $0 indicates the name of the script. • $1 indicates the 1st argument of that script. • $2 indicates the 2nd argument. • $$ used to denote the process ID. • $# used to count the number of arguments. • $* denotes all arguments.
  • 26. A simple example, var1=$1 var2=$2 var3=` expr $var1 + $var2 ` echo $var3 chmod +x file.sh ./abcd.sh 2 3 Output, 5
  • 27. In the previous example, If we do not give command line arguments, If we give more than 2 arguments,
  • 28. Using argument status, var1=$1 chmod +x file1.sh var2=$2 ./file1.sh 2 3 var3=`expr $var1 + output, $var2` 5 if [$# -ne 2 ] then ./file1.sh 2 3 4 echo “no” Output, exit 1 no else echo $var3 fi