A.5 Other Useful Commands


This section briefly describes other commands that you may encounter in system scripts.

A.5.1 set

The 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 eval

The 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 printf

The 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:

  • The minus-sign flag tells the command to pad the output on the right rather than the left (in other words, make the field left-aligned rather than right-aligned).

  • The plus-sign flag indicates that positive numbers should be preceded by an explicit plus sign. The space flag similarly indicates that positive numbers should be preceded by a space. These flags are useful for creating columns of aligned numbers regardless of sign (the default is not to place any character in front of a positive number). Note that this is an issue only when items are left-aligned.

  • The 0 flag indicates that zeros should be used for padding instead of blanks.

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 expr

The 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 arithmetic

Integer 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.

Table A-3. bash integer operators

Math operator

Meaning

 

Bitwise operator

Meaning

 

Logical operator

Meaning

+

add

 

>>

shift right

 

&&

logical AND

-

subtract

 

<<

shift left

 

||

logical OR

*

multiply

 

&

bitwise AND

 

==

equals

/

divide

 

|

bitwise OR

 

!=

not equals

%

modulus

 

~

bitwise NOT

 

<, <=

less than (or =)

**

exponentiation

 

^

bitwise XOR

 

>, >=

greater than (or =)

++

increment

       

!

logical NOT

--

decrement

           

c?t:f

conditional assignment

           

A few notes on these operators:

  • In general, the operators have the same meanings and precedence as they do in C. Parentheses should be used for explicit grouping.

  • The increment and decrement operators (++ and --) may either precede or follow the variable to which they are applied: var++ or ++var. Their placement determines whether the variable is modified before or after it is used.

  • The conditional assignment operator tests the condition (c), returning the value t if it is true or f if it is false.

  • Finally, the unary and bitwise operators can precede the equal sign in an assignment statement. For example, this statement adds three to the current value of counter:

    counter += 3
  • Note that only integer values are returned by integer expressions. Thus, 5 / 10 = 0.

A.5.4.2 bash arrays

The 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.



Essential System Administration
Essential System Administration, Third Edition
ISBN: 0596003439
EAN: 2147483647
Year: 2002
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net