0% found this document useful (0 votes)
18 views

10 Basic Script Elements (Control

The document discusses different control structures in scripting languages including if/else conditional statements, case statements for multiple conditional branches, while and until loops for iterations, and for loops for processing lists. Specific details provided on the syntax and flow of each structure.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

10 Basic Script Elements (Control

The document discusses different control structures in scripting languages including if/else conditional statements, case statements for multiple conditional branches, while and until loops for iterations, and for loops for processing lists. Specific details provided on the syntax and flow of each structure.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Revision no.

: PPT/2K403/02

Basic Script Elements


(Control Structures)
Revision no.: PPT/2K403/02

Basic Script Elements (Control Structures)


2

• Simple Branching with if.

• Multiple Branches with case.

• Iterations and Loops.

• Exiting from a Loop.

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Simple Branching with if


3

– Simple Branch within a Script


• if condition
• then
• commands
• fi
– if statement can be extended with an optional else :
• if condition
• then
• command1
• else
• command2
• fi

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Simple Branching with if (contd.)


4

– Branch created with if,represented in program flow chart :


start

no
command

yes

command1 command2

stop

– A branch of this type must begin with if and end with fi.
– The exit status of first command decides whether the command
introduced by then will executed. (exit status is Zero or not)
– If the status is not zero, shell goes to the end of the branch or, if
present to else

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Short Forms of if
5

– Various ways of executing several commands in sequence,this


includes the separators “&&” and “||”
• command1 && command2
• command || command2
– This separators can also be used as short forms of an if branch
Therefore, the structure
• if test –e file
• then
• .file
• fi
– can be condensed into the following command line:
• test –e file && .file
– Therefore you can replace the complex if –then –fi structure with
command line that uses “&&” or “||” to chain commands.

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Multiple Branches with case


6

– Apart from simple branches with if, it is possible to create multiple


branches with case.

– In case statement each variable is compared with expressions ,


for each matched expression a command is executed .

– A case statement as following structure:


• case $ VAR in

• expression1) command 1 ;;

• expression2) command2 ;;

• …

• esac

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Multiple Branches with case (contd.)


7

• Flow Chart for Multiple Branching with case:

start

Variable?

Value 1 Value 2 Value 3

command1 command2 command3

stop

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Iterations and Loops


8

• Looping with while and until


– The purpose of a loop is to test a condition and execute given
command “while” the condition is true (while loop) or “until” the
condition becomes true(until loop) .

while condition until condition


do do
commands commands
done done

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Iterations and Loops (contd.)

• Structure of a while or until loop


start

operation

terminating
condition B

– These loops actually rely on the exit status of terminating


conditions: while loop is operative as long as condition's exit
status is zero(path B in flow char)
– until loop is terminated if status is zer0(path A in flow chart)while
loop terminates when status is non-zero.
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Iterations and Loops (contd.)

10

• Processing a List with for


– The purpose of for loop is to process a list of elements,syntax is ;
• for variable in element1 elment2 elment3
• do
• commands
• done
– A for loop executes commands once for every element of the list
and value of the variable matches one list element with each loop
iteration.
– A command separator must immediately precede the “do” and
“done” parts of the for loop.
• for ex:
• for i in 1 2 3 4 5 6; do ping –c1 earth$i; done

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

Exiting From a Loop


11

• Exiting From the current loop iteration with continue and


break.
– The continue command makes it possible to exit from the current
iteration of a loop (while,until,for, and select)to resume with the
next iteration of the loop.

– continue command allows exiting from the current iteration of a


loop if the condition is true.

– With break command, a loop can terminated altogether according


to given condition.

© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute
Revision no.: PPT/2K403/02

12

Design & Published by:


CMS Institute, Design & Development Centre, CMS House, Plot No. 91, Street No.7,
MIDC, Marol, Andheri (E), Mumbai –400093, Tel: 91-22-28216511, 28329198
Email: [email protected]
www.cmsinstitute.co.in
© CMS INSTITUTE, 2004. All rights reserved. No part of this material may be reproduced, stored or emailed without the prior permission of Programme Director, CMS Institute

You might also like