This section briefly describes other commands that you may encounter in system scripts. A.5.1 setThe set command sets the values of $1 through $n to the words given as its arguments. It is often used with a backquoted command to assign the argument identifiers to the command's output. Here is an example of its use: $ who -r . run-level 2 Aug 21 16:58 2 0 S $ set `who -r` $ echo $6 16:58 The unset command may be used to remove a defined variable. A.5.2 evalThe eval command executes its argument as a shell command. It is used to execute commands generated by multiple levels of indirection. Here is a silly example: $ a=c; b=m; c=d; cmd=date $ echo $a$b$c cmd $ eval $`echo $a$b$c` Sun Jun 3 19:37:30 EDT 2001 Here is a real example that we looked at in Chapter 12: $ eval `tset -sQ -m ":?vt100"` This eval command runs the commands generated by tset -s. As we say, they are used to set the TERM and TERMCAP environment variables. The command eval resize provides a similar example for xterm windows. A.5.3 printfThe printf command is used to produce formatted output strings, and you will occasionally see it used in system scripts. It takes two arguments: a format-specification string and a list of items to be printed using that format. Here is an example command used to create a record in a printer accounting file: # pages=21; host=hamlet; user=chavez # printf '%7.2f\t%s:%s\n' "$pages" "$host" "$user" 21.00 hamlet:chavez This command creates a line in which the number of pages is printed as a floating point number containing two decimal places, followed by a tab and then the hostname and username joined by a colon. Format specification strings are comprised of field definitions and literal characters, and each successive item in the print list is formatted according to the corresponding field in the format string. In our example, %7.2f and %s (twice) were the field definitions, and the tab (\t), colon, and newline character (\n) were literal characters. Field definitions always begin with a percent sign. Their simplest syntax is: %n[.m]z n indicates the minimum width of the field, m indicates the number of decimal places (if applicable), and z is a code letter indicating the type of field data. The most important codes are d for signed integer, f for floating point, c for the first character of the argument, s for a character string and x or X for a hexadecimal number (depending on whether you want the alphabetic digits to appear in lowercase or uppercase). A percent sign is specified with %%. At output time, field widths are automatically expanded when more space is needed, and output that is smaller than the specified width is padded on the left. The printf command also allows some optional flags to be placed between the percent sign and the field width:
Here are some examples illustrating some of these flags: # n=27; n1=-23 # printf '*%7.1f* *%-7.1f* \n' $n $n * 27.0* *27.0 * # printf '%-5.1f\n%-5.1f\n%-+5.1f\n%- 5.1f\n' $n $n1 $n $n 27.0 -23.0 +27.0 27.0 A.5.4 exprThe expr command is used to evaluate various expressions. It has a lot of uses, but one common one in shell scripts is integer arithmetic. Here is a very simple example of its use in this mode: $ cat count_to_5 #!/bin/sh i=1 while [ $i -le 5 ] ; do echo $i i=`expr $i + 1` # add one to i done $ count_to_5 1 2 3 4 5 See the manual page for full details on expr. A.5.4.1 bash integer arithmeticInteger arithmetic is included within the bash shell (so we can hope that constructions like the preceding will eventually go away). Here are some simple examples: $ echo $(( 5+8/2-1 )) 8 $ a='1+2'; echo $a 1+2 $ let a='1+2'; echo $a 3 $ declare -i a; a='1+2'; echo $a 3 The first command illustrates the $(( )) operator, which forces the enclosed expression to be interpreted as integer arithmetic. Note that the usual operator precedence rules apply. The second command illustrates that simply constructing an integer expression is not sufficient for it to be evaluated. You must use the $(( )) operator, precede the variable with let, or declare the variable to be of type integer (indicated by -i). The declare command may also be used to specify other variable types (see the documentation for details). Table A-3 lists the supported arithmetic operators.
A few notes on these operators:
A.5.4.2 bash arraysThe bash shell also supports array variables. They are not very prevalent in system scripts at present, so we will present just a brief overview of their use via some examples: $ a=(aaa bbb [5]=eee ddd) Define an array and some values. $ echo ${a[4]} ${a[5]} ddd eee $ echo ${a[3]:-undefined} undefined Arrays can have "holes": undefined elements. $ a=(x y z); echo ${a[4]:-undefined} undefined Redefining an array replaces all elements. $ for i in ${a[@]}; do Loop over array elements. > echo $i; done x y z $ echo ${#a[@]} Number of non-null elements in array a. 3 See the bash documentation for more information about arrays. |