Command-Line Options
Positional Parameters
Command-line options may be referenced with the usual positional parameters
- Arguments are separated by spaces on the command line
- Each parameter is passed a string argument (or is NULL)
- By convention, options are listed before all other arguments
Problem: if options are truly optional, how do we keep track of what
the positional parameters represent?
shift
The shift command has the effect of shifting all of the positional parameters
over by a specified number
- By itself, shift will shift all arguments over by one
- With a numeric argument n, shift will shift all arguments over by n
- Options with arguments may be handled by shifting twice
Option Processing
The getopts built-in handles multiple complex options.
- Allows options to be combined with a single dash
ls -lta
- Allows an option argument to follow the option without a separating
space
cut -d: -f5-8
- Processes options in sequence
- OPTIND is assigned the number of the next command-line argument to
be processed
getopts
getopts expects 2 arguments:
- a string containing letters and colons
- each letter represents a valid option
- a letter followed by a colon indicates that that option expects an
argument
- a colon as the 1st character indicates that invalid options should
not be reported
- a variable
If getopts successfully retrieves an option,
- the given variable is assigned a single character
- the option character, if it is valid
- '?' otherwise
- OPTARG is assigned the option's argument
- getopts returns an exit status of 0
Example
(from Learning the Korn Shell, p. 163)
while getopts ":cgl:o:O" opt; do
case $opt in
c ) do_link=false ;;
g ) debug="-g" ;;
l ) link_libs="$link_libs -l $OPTARG"
;;
o ) exefile="-o $OPTARG" ;;
O ) opt=true ;;
\? ) print "usage: $0 [-cgO] [-l lib] [-o
file] files"
return 1 ;;
esac
done
shift $(($OPTIND - 1))
Integer Arithmetic
Arithmetic Expressions
Korn shell interprets words within $(( and )) as arithmetic expressions.
- evaluated within double quotes ("")
- supports C arithmetic operators and precedence
- + - * / %
- << >> & | ~ ^
- parentheses may group subexpressions
- don't need to quote special characters
- supports C assignment operators
- supports C relational operators and precedence
- < > <= >= == != && ||
- true expressions = 1; false = 0
Alternative: use the expr unix command
Arithmetic Conditionals
Korn shell interprets words within (( and )) as arithmetic condition
tests.
- true values produce an exit status of 0;
false values produce an exit status of 1
- may also use numeric values in place of truth values
- 0 = false ( -> exit status 1)
- non-0 = true ( -> exit status 0)
Arithmetic Variables and Assignment
Arithmetic variables may be defined and assigned values one of two ways:
- (( intvar=expression ))
- let intvar=expression
Must quote special characters and expressions that contain spaces
Arrays
Ordered one-dimensional list of <= 1024 elements
- index specified within [ and ]
- assign individual elements
- nickname[2]=bob
- array elements with unassigned values don't exist
- initialize an array with a sequence of values
- access array values
- ${aname[i]} yields the value of element i
- ${aname[*]} yields the values of all assigned element, separated by
spaces
- ${aname[@]} preserves spaces in elements
- array size
- ${#aname[*]} provides the number of elements in aname
- ${#aname} provides the length of aname[0]
Variable Specifications
The typeset command is used to
- specify formatting for a string variable
- set the type and attributes of variables
- work with functions
Without options, typeset varname inside a function will make
variable varname local to that function.
Without specified varname(s), the shell prints a list of variables for
which the given option is turned on.
String Formatting
Calling typeset with string formatting options imposes a format on the
contents of the named variable(s). Options are cumulative.
- typeset -Ln varname - left-justify, removing leading
blanks; if n is given, fill with blanks or truncate to length n
- typeset -Rn varname - right-justify, removing trailing
blanks; if n is given, fill with blanks or truncate to length n
- typeset -Zn varname - same as right-justify, but fill
with leading 0's instead of blanks when n is given
- typeset -l varname - convert all letters to
lowercase
- typeset -u varname - convert all letters to uppercase
Type & Attribute Options
- typeset -in varname - represent the variable internally
as an integer; if n is given, use it as the base used for output
- typeset -r varname - make the variable read-only
- typeset -x varname - export the variable
Function Options
These all relate to functions, not variables
- typeset -f fname - print the definition of the function;
if no function is specified, print all function definitions
- typeset +f - print all function names
- typeset -ft fname - turn on trace mode for the
function
- typeset +ft fname - turn off trace mode for the function
- typeset -fu fname - autoload the named function(s)