27.08.2015 Views

Advanced Bash−Scripting Guide

Advanced Bash-Scripting Guide - Nicku.org

Advanced Bash-Scripting Guide - Nicku.org

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Advanced</strong> <strong>Bash−Scripting</strong> <strong>Guide</strong><br />

declare, typeset<br />

The declare and typeset commands specify and/or restrict properties of variables.<br />

readonly<br />

Same as declare −r, sets a variable as read−only, or, in effect, as a constant. Attempts to change the<br />

variable fail with an error message. This is the shell analog of the C language const type qualifier.<br />

getopts<br />

This powerful tool parses command−line arguments passed to the script. This is the Bash analog of<br />

the getopt external command and the getopt library function familiar to C programmers. It permits<br />

passing and concatenating multiple options [25] and associated arguments to a script (for example<br />

scriptname −abc −e /usr/local).<br />

The getopts construct uses two implicit variables. $OPTIND is the argument pointer (OPTion INDex)<br />

and $OPTARG (OPTion ARGument) the (optional) argument attached to an option. A colon following<br />

the option name in the declaration tags that option as having an associated argument.<br />

A getopts construct usually comes packaged in a while loop, which processes the options and<br />

arguments one at a time, then decrements the implicit $OPTIND variable to step to the next.<br />

1. The arguments passed from the command line to the script must be preceded<br />

by a minus (−) or a plus (+). It is the prefixed − or + that lets getopts<br />

recognize command−line arguments as options. In fact, getopts will not<br />

process arguments without the prefixed − or +, and will terminate option<br />

processing at the first argument encountered lacking them.<br />

2. The getopts template differs slightly from the standard while loop, in that it<br />

lacks condition brackets.<br />

3. The getopts construct replaces the obsolete and less powerful getopt external<br />

command.<br />

while getopts ":abcde:fg" Option<br />

# Initial declaration.<br />

# a, b, c, d, e, f, and g are the options (flags) expected.<br />

# The : after option 'e' shows it will have an argument passed with it.<br />

do<br />

case $Option in<br />

a ) # Do something with variable 'a'.<br />

b ) # Do something with variable 'b'.<br />

...<br />

e) # Do something with 'e', and also with $OPTARG,<br />

# which is the associated argument passed with option 'e'.<br />

...<br />

g ) # Do something with variable 'g'.<br />

esac<br />

done<br />

shift $(($OPTIND − 1))<br />

# Move argument pointer to next.<br />

# All this is not nearly as complicated as it looks .<br />

Example 11−17. Using getopts to read the options/arguments passed to a script<br />

Chapter 11. Internal Commands and Builtins 138

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!