Presentation on Looping Conditions andCommand Line Arguments
Conditional loop• conditional loop is a way for computer  programs to repeat one or more various  steps depending on condi...
While Loop• The while loop is used to execute a set of  commands repeatedly until some  condition occurs.• It is usually u...
Basic syntaxWhile commanddo    listdoneIt can also be written as,While command ; do list ; done
Steps to execute a while loop1. Execute command.2. If the exit status of command is nonzero,   exit from the while loop3. ...
For example,                              The output looks like,x=0                           0while [ $x –lt 10 ]        ...
Example(2)c=1                     Output,while [ $c -le 5 ]      Welcome 1 times do                     Welcome 2 times   ...
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 rest...
Syntax is,while command1 ; # this is loop1, the outer loopdo    list1    while command2 ; # this is loop2, the inner  loop...
Example,x=0while [ "$x" -lt 10 ] ; # this is loop1 do    y="$x"    while [ "$y" -ge 0 ] ; # this is loop2     do         e...
The output will be,0102103210432105432106543210765432108765432109876543210
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 ...
The common syntax is,for name in word1 word2 ... wordN  do     list  doneIt can also be written as,for name in word1 word2...
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            ...
another example,for i in `cat 1.txt`     Output is, do     echo $i             F  done                   S                ...
Example(3)alphabet="a b c d e"          Output,count=0                       afor letter in $alphabet                     ...
Command line arguments• The arguments used to pass to the shell  scripts while interpreting are called  command line argum...
A simple example,var1=$1var2=$2var3=` expr $var1 + $var2 `echo $var3 chmod +x file.sh./file.sh 2 3Output,5
Using argument status,var1=$1               chmod +x file1.shvar2=$2              ./file1.sh 2 3var3=`expr $var1 +    outp...
Any Queries????
Thank You
Upcoming SlideShare
Loading in...5
×

Scripting ppt

1,250

Published on

ppt of looping conditions

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,250
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "Scripting ppt"

  1. 1. Presentation on Looping Conditions andCommand Line Arguments
  2. 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. 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. 4. Basic syntaxWhile commanddo listdoneIt can also be written as,While command ; do list ; done
  5. 5. Steps to execute a while loop1. Execute command.2. If the exit status of command is nonzero, exit from the while loop3. If the exit status of command is zero, execute list.4. When list finishes execution, return to step 1.
  6. 6. For example, The output looks like,x=0 0while [ $x –lt 10 ] 1do 2 echo $x 3 x=`echo “$x + 1” | bc` 4done 5 6 7 8 9
  7. 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. 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. 9. Syntax is,while command1 ; # this is loop1, the outer loopdo list1 while command2 ; # this is loop2, the inner loop do list2 done list3done
  10. 10. Example,x=0while [ "$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. 11. The output will be,0102103210432105432106543210765432108765432109876543210
  12. 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. 13. The common syntax is,for name in word1 word2 ... wordN do list doneIt can also be written as,for name in word1 word2 ... wordN ; do list ; done
  14. 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. 15. another example,for i in `cat 1.txt` Output is, do echo $i F done S TThe contents in 1.txt,FST
  16. 16. Example(3)alphabet="a b c d e" Output,count=0 afor letter in $alphabet b do count=`expr $count + 1` c echo "Letter $count is d [$letter]" e done
  17. 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. 18. A simple example,var1=$1var2=$2var3=` expr $var1 + $var2 `echo $var3 chmod +x file.sh./file.sh 2 3Output,5
  19. 19. Using argument status,var1=$1 chmod +x file1.shvar2=$2 ./file1.sh 2 3var3=`expr $var1 + output, $var2` 5if [$# -ne 2 ]then ./file1.sh 2 3 4echo “no” Output,exit 1 noelseecho $var3fi
  20. 20. Any Queries????
  21. 21. Thank You
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×