SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

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
 

Tendances (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
 

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Dernier

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
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

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)
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
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
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
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...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
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
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
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
 
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
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 

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