SlideShare ist ein Scribd-Unternehmen logo
1 von 21
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.
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
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)
alphabet="a b c d e"          Output,
count=0                       a
for letter in $alphabet
                              b
 do
  count=`expr $count + 1`     c
  echo "Letter $count is      d
  [$letter]"                  e
 done
Command line arguments
• The arguments used to pass to the shell
  scripts while interpreting are called
  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
./file.sh 2 3

Output,
5
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

Weitere ähnliche Inhalte

Was ist angesagt?

Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
Iftekhar Eather
 
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
Tom Corrigan
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
brian d foy
 

Was ist angesagt? (20)

Let's golang
Let's golangLet's golang
Let's golang
 
Sol8
Sol8Sol8
Sol8
 
Scripting 101
Scripting 101Scripting 101
Scripting 101
 
Introducing PHP Latest Updates
Introducing PHP Latest UpdatesIntroducing PHP Latest Updates
Introducing PHP Latest Updates
 
ZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 VersionZeroMQ Is The Answer: DPC 11 Version
ZeroMQ Is The Answer: DPC 11 Version
 
ZeroMQ Is The Answer
ZeroMQ Is The AnswerZeroMQ Is The Answer
ZeroMQ Is The Answer
 
Bag of tricks
Bag of tricksBag of tricks
Bag of tricks
 
Cis 216 – shell scripting
Cis 216 – shell scriptingCis 216 – shell scripting
Cis 216 – shell scripting
 
Process monitoring in UNIX shell scripting
Process monitoring in UNIX shell scriptingProcess monitoring in UNIX shell scripting
Process monitoring in UNIX shell scripting
 
Perl Bag of Tricks - Baltimore Perl mongers
Perl Bag of Tricks  -  Baltimore Perl mongersPerl Bag of Tricks  -  Baltimore Perl mongers
Perl Bag of Tricks - Baltimore Perl mongers
 
The Magic Of Tie
The Magic Of TieThe Magic Of Tie
The Magic Of Tie
 
Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本Shell实现的windows回收站功能的脚本
Shell实现的windows回收站功能的脚本
 
Generating Power with Yield
Generating Power with YieldGenerating Power with Yield
Generating Power with Yield
 
Redis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your applicationRedis & ZeroMQ: How to scale your application
Redis & ZeroMQ: How to scale your application
 
Tugas Program C++
Tugas Program C++Tugas Program C++
Tugas Program C++
 
ZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made SimpleZeroMQ: Messaging Made Simple
ZeroMQ: Messaging Made Simple
 
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
 
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer[2019] 아직도 돈 주고 DB 쓰나요? for Developer
[2019] 아직도 돈 주고 DB 쓰나요? for Developer
 
Advanced modulinos
Advanced modulinosAdvanced modulinos
Advanced modulinos
 
Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019Symfony messenger - PHPers Summit 2019
Symfony messenger - PHPers Summit 2019
 

Andere mochten auch

Scripting powerpoint
Scripting powerpointScripting powerpoint
Scripting powerpoint
missSbennett
 
Unitat4
Unitat4Unitat4
Unitat4
Sergi
 
Problemas de genética
Problemas de genéticaProblemas de genética
Problemas de genética
CC NN
 
Reproducció sexual animals i plantes: Alicia, Irene, Raquel
Reproducció sexual animals i plantes: Alicia, Irene, RaquelReproducció sexual animals i plantes: Alicia, Irene, Raquel
Reproducció sexual animals i plantes: Alicia, Irene, Raquel
romanica2neso
 
Two dimensional geometric transformations
Two dimensional geometric transformationsTwo dimensional geometric transformations
Two dimensional geometric transformations
Mohammad Sadiq
 

Andere mochten auch (20)

Scripting powerpoint
Scripting powerpointScripting powerpoint
Scripting powerpoint
 
Loop
LoopLoop
Loop
 
Toolbar
ToolbarToolbar
Toolbar
 
Script
ScriptScript
Script
 
Unitat4
Unitat4Unitat4
Unitat4
 
La reproducció i el desenvolupament dels organismes pluricel·lulars
La reproducció i el desenvolupament dels organismes pluricel·lularsLa reproducció i el desenvolupament dels organismes pluricel·lulars
La reproducció i el desenvolupament dels organismes pluricel·lulars
 
Problemas de genética
Problemas de genéticaProblemas de genética
Problemas de genética
 
9.La reproducció i la relació de la cèl·lula eucariota
9.La reproducció i la relació de la cèl·lula eucariota9.La reproducció i la relació de la cèl·lula eucariota
9.La reproducció i la relació de la cèl·lula eucariota
 
Reproducció sexual animals i plantes: Alicia, Irene, Raquel
Reproducció sexual animals i plantes: Alicia, Irene, RaquelReproducció sexual animals i plantes: Alicia, Irene, Raquel
Reproducció sexual animals i plantes: Alicia, Irene, Raquel
 
La reproducció i el desenvolupament
La reproducció i el desenvolupamentLa reproducció i el desenvolupament
La reproducció i el desenvolupament
 
03loop conditional statements
03loop conditional statements03loop conditional statements
03loop conditional statements
 
Genètica i evolució II
Genètica i evolució IIGenètica i evolució II
Genètica i evolució II
 
82. La reproducció asexual i sexual
82. La reproducció asexual i sexual82. La reproducció asexual i sexual
82. La reproducció asexual i sexual
 
Column writing
Column writingColumn writing
Column writing
 
Dramatic Features of a Play.
Dramatic Features of a Play.Dramatic Features of a Play.
Dramatic Features of a Play.
 
Two dimensional geometric transformations
Two dimensional geometric transformationsTwo dimensional geometric transformations
Two dimensional geometric transformations
 
Column writing
Column writingColumn writing
Column writing
 
Otto kernberg
Otto kernbergOtto kernberg
Otto kernberg
 
Editorial Writing 101
Editorial Writing 101Editorial Writing 101
Editorial Writing 101
 
Chl mj 164_ac
Chl mj 164_acChl mj 164_ac
Chl mj 164_ac
 

Ähnlich wie Scripting ppt

Lecture 22
Lecture 22Lecture 22
Lecture 22
rhshriva
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
erbipulkumar
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
Apoorvi Kapoor
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Anton Shemerey
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
Raghu nath
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
Raghu nath
 

Ähnlich wie Scripting ppt (20)

Unit vii wp ppt
Unit vii wp pptUnit vii wp ppt
Unit vii wp ppt
 
Syntax
SyntaxSyntax
Syntax
 
Lecture 22
Lecture 22Lecture 22
Lecture 22
 
Shell Scripting
Shell ScriptingShell Scripting
Shell Scripting
 
Mips1
Mips1Mips1
Mips1
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Lecture19-20
Lecture19-20Lecture19-20
Lecture19-20
 
Module 03 Programming on Linux
Module 03 Programming on LinuxModule 03 Programming on Linux
Module 03 Programming on Linux
 
Shell programming
Shell programmingShell programming
Shell programming
 
tutorial7
tutorial7tutorial7
tutorial7
 
tutorial7
tutorial7tutorial7
tutorial7
 
2-introduction_to_shell_scripting
2-introduction_to_shell_scripting2-introduction_to_shell_scripting
2-introduction_to_shell_scripting
 
Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02Ruby19 osdc-090418222718-phpapp02
Ruby19 osdc-090418222718-phpapp02
 
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code styleRuby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
Ruby: OOP, metaprogramming, blocks, iterators, mix-ins, duck typing. Code style
 
Bash Shell Scripting
Bash Shell ScriptingBash Shell Scripting
Bash Shell Scripting
 
Linux Shell Scripting
Linux Shell ScriptingLinux Shell Scripting
Linux Shell Scripting
 
Ruby
RubyRuby
Ruby
 
php programming.pptx
php programming.pptxphp programming.pptx
php programming.pptx
 
Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!Hypers and Gathers and Takes! Oh my!
Hypers and Gathers and Takes! Oh my!
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 

Kürzlich hochgeladen

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
fonyou31
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Kürzlich hochgeladen (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

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. Basic syntax While command do list done It can also be written as, While command ; do list ; done
  • 5. 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.
  • 6. 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
  • 7. 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
  • 8. 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.
  • 9. 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
  • 10. 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
  • 11. The output will be, 0 10 210 3210 43210 543210 6543210 76543210 876543210 9876543210
  • 12. 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.
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. Example(3) alphabet="a b c d e" Output, count=0 a for letter in $alphabet b do count=`expr $count + 1` c echo "Letter $count is d [$letter]" e done
  • 17. Command line arguments • The arguments used to pass to the shell scripts while interpreting are called 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.
  • 18. A simple example, var1=$1 var2=$2 var3=` expr $var1 + $var2 ` echo $var3 chmod +x file.sh ./file.sh 2 3 Output, 5
  • 19. 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