Scripting ppt

21
Presentation on Looping Conditions and Command Line Arguments

description

ppt of looping conditions

Transcript of Scripting ppt

Page 1: Scripting ppt

Presentation on Looping Conditions and

Command Line Arguments

Page 2: Scripting ppt

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.

Page 3: Scripting ppt

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.

Page 4: Scripting ppt

Basic syntax

While command do list done

It can also be written as,

While command ; do list ; done

Page 5: Scripting ppt

Steps to execute a while loop

1. 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.

Page 6: Scripting ppt

For example,

x=0while [ $x –lt 10 ] do echo $x x=`echo “$x + 1”

| bc` done

The output looks like,0123456789

Page 7: Scripting ppt

Example(2)

c=1 while [ $c -le 5 ] do echo "Welcome $c

times" (( c++ )) done

Output,Welcome 1

timesWelcome 2

timesWelcome 3

timesWelcome 4

timesWelcome 5

times

Page 8: Scripting ppt

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.

Page 9: Scripting ppt

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

Page 10: Scripting ppt

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

Page 11: Scripting ppt

The output will be,

01 02 1 03 2 1 04 3 2 1 05 4 3 2 1 06 5 4 3 2 1 07 6 5 4 3 2 1 08 7 6 5 4 3 2 1 09 8 7 6 5 4 3 2 1 0

Page 12: Scripting ppt

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.

Page 13: Scripting ppt

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

Page 14: Scripting ppt

For a simple for loop,

for i in 0 1 2 3 4 5 6 7 8 9 do echo $i done

The output is,0123456789

Page 15: Scripting ppt

another example,

for i in `cat 1.txt` do echo $i done The contents in 1.txt,FST

Output is,

FST

Page 16: Scripting ppt

Example(3)alphabet="a b c d e" count=0 for letter in $alphabet do count=`expr $count + 1` echo "Letter $count is

[$letter]" done

Output, a b c d e

Page 17: Scripting ppt

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.

Page 18: Scripting ppt

A simple example,

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

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

Output,5

Page 19: Scripting ppt

Using argument status,

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

$var2` if [$# -ne 2 ] then echo “no” exit 1 else echo $var3 fi

chmod +x file1.sh./file1.sh 2 3 output,5

./file1.sh 2 3 4Output,no

Page 20: Scripting ppt

Any Queries????

Page 21: Scripting ppt

Thank You