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

11 Advanced Scripting Techniques

1) The document discusses various advanced scripting techniques in Bash including reading input with read, defining shell functions, reading command line options with getopts, handling signals with trap, creating menus with select, and displaying dialog boxes with dialog. 2) Key points covered include using read to accept user input, defining reusable functions, parsing options with getopts and case, trapping signals to avoid undesirable behavior, implementing simple menus in select loops, and different dialog box types like yesno. 3) The techniques described allow for more advanced interaction and control flow in Bash shell scripts.

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)
31 views

11 Advanced Scripting Techniques

1) The document discusses various advanced scripting techniques in Bash including reading input with read, defining shell functions, reading command line options with getopts, handling signals with trap, creating menus with select, and displaying dialog boxes with dialog. 2) Key points covered include using read to accept user input, defining reusable functions, parsing options with getopts and case, trapping signals to avoid undesirable behavior, implementing simple menus in select loops, and different dialog box types like yesno. 3) The techniques described allow for more advanced interaction and control flow in Bash shell scripts.

Uploaded by

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

Revision no.

: PPT/2K403/02

Advanced Scripting Techniques


Revision no.: PPT/2K403/02

Advanced Scripting Techniques


2

• Reading Input with read.

• Shell Functions.

• Reading options with getopts.

• Signal handling with trap.

• Implementing simple Menus with select.

• Dialog Boxes with Dialog

© 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 Input with read


3

– read can be used to read simple string or number,for ex


• echo “please read the number”
• read NUMBER
– read command is also able to read several lines of input,
The syntax is;
commands | while read VAR1 VAR2 while read VAR1 VAR2
do do
commands commands
done done < file

– With while loop, reads process the input line by line as available
from output of a command or from file.

© 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

Shell Functions
4

– Shell function are defined at beginning of a Script.


– Shell function make whole script available under a single name.
– The syntax to define a function;
• functionname ( ) {
• commands
• commands
• }

– The function may be composed of any character string that then


can be used to call the function.

© 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 Options With getopts


5

– getopts is shell inbuilt in command, used to extract the options


supplied to script on the command line.
– Syntax for command is ; getopts optionstring VAR
– The optionstring describes all options to be specified,it is
followed by variable to which all command-line options specified
are assigned.
– The getopts command is mostly used in while loop together with
case as follows;
• while getopts optionstring VAR; do
• case $VAR in
• optionstring1 ) command1 ;;
• optionstring2 ) command2 ;;
• esac
• 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

Signal Handling with trap


6

– Signals can be sent to any running process on the system with kill
command.
– Signal available on the system can be listed with kill –l .
– With exception of signal 9, Linux kernel sends all signals to given
process.
– Following shows some common signals;
• Signal 1 –SIGHUP-Shells send signal to all processes started by it.
• Signal 2 –SIGINT-The signal requests the process to exit.
• Signal 9 –SIGKILL-This signal is not handled by process but directly
by Kernel
• Signal 15 –SIGTERM- This is default signal started by Kill command.

© 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

Signal Handling with trap (contd.)

• Signal 18 –SIGCONT- This signal causes a process with the status “T”
(stopped or traced ) to continue.
• Signal 19 – SIGSTOP- It causes the process to suspended giving it
the status “T”.

– Shell scripts may behave in undesirable way,if it happens script


might leave a number of temporary files.
– The trap command helps to avoid this by running certain
commands.
– The trap command requires following syntax:
• trap command signal

© 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

Implementing Simple Menus with select


8

– With BASH , it is possible to create simple menus with help of


select built-in command.
– The syntax of select command;
• PS3=prompting-text
• select VARIABLE in item1 item2 item3
• do
• commands
• done

– In most cases, a case construct is used within the select loop to


define command for each item.

© 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

Dialog Boxes with dialog


9

– The dialog package allows you to present different types of


boxes,for different task.

– Before dialog can created from shell script, you need install dialog
as an additional package.

– The dialog command requires this syntax:

– dialog –boxtype “text” height width

– Through KDE desktop, it is possible to create “real” graphical


user interface elements with kdialog.

© 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

Dialog Boxes with dialog (contd.)

10

• Yes/No Box (yesno)


– The box displays some text and two buttons below for selecting
yes or no, Ex:
• dialog --yesno “Would you like to continue?” 10 50

– You can define also title for the box with - -title,

• dialog –title “yesno box” –yesno “would you like to continue?” 10 50

© 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

Dialog Boxes with dialog (contd.)

11

• Message Box (msgbox)

– This type of box allows you to display some text to user to

confirm the message by selecting ok button.

• dialog --msgbox “This is a message.” 10 50

© 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

Dialog Boxes with dialog (contd.)

12

• Input Box (input box)

– This type of dialog box, prompt the user to supply some kind of

input.

– The input is not directly processed by stored in to temporary file.

• dialog –inputbox “Please enter something.” 10 50

© 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

Dialog Boxes with dialog (contd.)

13

• Text Box (textbox)

– This type of box, displays the contents of text files.

• dialog –textbox /etc/passwd 10 50

– This displays the file /etc/passwd in text box.

– The user can close the box by selecting EXIT button.

© 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

Dialog Boxes with dialog (contd.)

14

• Menu Box (menu)

– This type of box,presents user with menu to select one item from

the list.

• dialog --menu “This is a menu.” 10 50 2 \

• “option 1” “This is this the first option” \

• “option 2” “This is this the second option”

© 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

Dialog Boxes with dialog (contd.)

15

• Check List Box (checklist)


– It is similar to menu box it presents a number of entries from
which to select.

– Any number of check boxes in the list can be activated or


deactivated.

• dialog –checklist “This is a checklist” 10 50 2 \

• “a” “This is one option” “off” \

• “b” “This is the second option” “on”

© 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

Dialog Boxes with dialog (contd.)

16

• Radio List Box (radiolist)

– It is similar to checklist box with the difference that only one of

the entries can be active at a time.

• dialog –radiolist “This is a selective list,where only one \

• option can be chosen” 10 50 2 \

• “a” “This is the first option” “off” \.

• “b” “This is the second option” “on”

© 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

Dialog Boxes with dialog (contd.)

17

• Progress Meter Box (gauge)


– It allows you to display a gauge displaying the progress of a given
process.
– For this dialog requires that the output of the process is available
in numerical format.
– The following script shows how it is implemented:
• #!/bin/bash
• declare –i COUNTER=1
• {
• while test $COUNTER –1e 100
• do
• echo $COUNTER
• COUNTER=COUNTER+1
• sleep 1

© 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

Dialog Boxes with dialog (contd.)

18

• done
• }| dialog –gauge “This is a progress bar” 10 50 0

© 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

19

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