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

09 Basic Script Elements (Input

The document discusses basic elements of scripting including program flow charts, general considerations when writing scripts, producing output from scripts, reading user input, and performing simple operations with variables. Program flow charts help lay out the steps a script will perform. A script's general structure includes a start, commands, and end. The echo command is commonly used to produce output while the read command allows getting user input.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

09 Basic Script Elements (Input

The document discusses basic elements of scripting including program flow charts, general considerations when writing scripts, producing output from scripts, reading user input, and performing simple operations with variables. Program flow charts help lay out the steps a script will perform. A script's general structure includes a start, commands, and end. The echo command is commonly used to produce output while the read command allows getting user input.

Uploaded by

Meet Mahida
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Revision no.

: PPT/2K403/02

Basic Script Elements(Input, Output)


Revision no.: PPT/2K403/02

Basic Script Elements (Input,Output)


2

• Program Flow Charts.

• General Considerations.

• Producing Output From a Script.

• Reading User Input.

• Simple Operations with Variables.

© 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

Program Flow Charts


3

– Program through a flow chart provides following benefits :


– They force the author to lay down steps the script should
perform to achieve the desired goal.
– They provide a clear symbolic outline of the algorithm, which can
be used as guide during programming process
– These are typical symbols used to create flow charts

operation

subroutine module

© 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

Program Flow Charts (contd.)


4

Input output

working

Boundary(start,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

General Considerations
5

– A shell script is basically an ASCII file containing commands that


should be executed in sequence.

– It can also run the script from another shell, with


command like sh script.sh .

– The directory where script is located must be actually in the


users’s search path for executable.

– When giving a file name generally append the “.sh” suffix to


it,this ensures that the file can be easily recognized as shell
scripts.

© 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

Producing Output from the Script


6

– Script contains three elements, the program start , the action, and
the program stop.
– General rules for creating a Script.
– Start
• The first line of any script should be the shebang (e.g # ! /bin/bash).
• This line specifies the shell program to call to execute the script.
– Commands
• Shell scripts in general rely on the echo command as the most
common solution to produce output.
– Stop
• At last it defines the script exit status with exit value , which informs
parents how the script was terminated.

© 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

Reading User Input


7

– One way to write script which accept user input is to use


command read.
– The user input is stored in a variable, which is then supplied to
read as an argument:
• read VARIABLE

start

output Please enter …

input read VARIABLE

output echo $VARIABLE


A Simple Script stop
Reading User Input
© 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 Operations with Variables


8

• Basic String Operations


start

output echo enter first name

read First name


input

echo enter Last name


output

read Last name


input

Name name=“$FIRSTNAME $LASTNAME”

output echo name is $name

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

A script performing Simple string Operation


Revision no.: PPT/2K403/02

Arithmetic Operations
9

– The Bourne shell is limited in arithmetic operations ,but can


perform such operations relying on external command(such as
expr)
– The Bash shell comes with a built in support for arithmetic
operations,but some limitations are there :
• Only operations with Whole numbers can be performed.
• All numbers are 32-bit values with no overflow check.
– Followings shows all possible methods and formats of arithmetic
operations.
– Example: A=B+10

– Using the external command expr(Bourne shell compatible)


• A=‘expr $B + 10’

© 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

Arithmetic Operations (contd.)


10

– With the expr command only five operators are available ”+”,”-”,
”*”,”/”, and “%”.
– Using the Bash built –in let :
• let A =“$B + 10”

– Arithmetic expressions inside parentheses or brackets:


• A=$((B + 10))
• A+$[B + 10]

– Using the built-in declare to declare a variable as integer:


• declare –i A
• declare –i B
• A=B+10

© 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

11

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