1 | .TH EXPR 1
|
---|
2 | .SH NAME \" Copyright (C) 1989 by Kenneth Almquist.
|
---|
3 | expr, test, [ \- evaluate expressions
|
---|
4 | .SH SYNOPSIS
|
---|
5 | .B expr
|
---|
6 | .I expression
|
---|
7 | .br
|
---|
8 | .B test
|
---|
9 | .I expression
|
---|
10 | .br
|
---|
11 | .B [
|
---|
12 | .I expression
|
---|
13 | .B ]
|
---|
14 | .SH DESCRIPTION
|
---|
15 | .B Expr
|
---|
16 | evaluates the expression and prints the result.
|
---|
17 | .B Test
|
---|
18 | evaluates the expression without printing the result.
|
---|
19 | The ``[''
|
---|
20 | command is a synonym for
|
---|
21 | .BR test ;
|
---|
22 | when invoked under this name
|
---|
23 | the last argument to
|
---|
24 | .B expr
|
---|
25 | must be a ``]'', which is deleted and not considered part of the expression.
|
---|
26 | .PP
|
---|
27 | Three data types may occur in the
|
---|
28 | .IR expression :
|
---|
29 | string, integer, and boolean.
|
---|
30 | The rules for conversion are as follows:
|
---|
31 | .sp
|
---|
32 | .nr i 2
|
---|
33 | .ta \nii
|
---|
34 | .in +\nii
|
---|
35 | .ti -\nii
|
---|
36 | \fIstring\fR\->\fIinteger\fR Done via
|
---|
37 | .BR atoi (3).
|
---|
38 | .ti -\nii
|
---|
39 | \fIinteger\fR\->\fIstring\fR Convert to decimal representation.
|
---|
40 | .ti -\nii
|
---|
41 | \fIstring\fR\->\fIboolean\fR "" \-> false, everything else to true.
|
---|
42 | .ti -\nii
|
---|
43 | \fIboolean\fR\->\fIstring\fR false \-> "", true \-> "true".
|
---|
44 | .ti -\nii
|
---|
45 | \fIinteger\fR\->\fIboolean\fR 0 \-> false, everything else to true.
|
---|
46 | .ti -\nii
|
---|
47 | \fIboolean\fR\->\fIinteger\fR false \-> 0, true \-> 1.
|
---|
48 | .in -\nii
|
---|
49 | .PP
|
---|
50 | Any argument to
|
---|
51 | .B expr
|
---|
52 | which is not a legal operator is treated as a string operand of type
|
---|
53 | .BR string .
|
---|
54 | .PP
|
---|
55 | As a special case, if
|
---|
56 | .I expression
|
---|
57 | is omitted, the result is false.
|
---|
58 | .PP
|
---|
59 | We now list the operators. The syntax
|
---|
60 | .sp
|
---|
61 | .ti +8
|
---|
62 | \fIinteger\fB op \fIinteger\fR \-> \fIboolean\fB (3)\fR
|
---|
63 | .sp
|
---|
64 | means that \fBop\fR is a binary operator which takes operands of type
|
---|
65 | \fIinteger\fR and produces a result of type \fIboolean\fR.
|
---|
66 | The ``(3)'' means that the priority of \fBop\fR is 3.
|
---|
67 | Operands are automatically converted to the appropriate type. The type
|
---|
68 | \fIany\fR is used for operator that take operands of any type.
|
---|
69 | .nr p 1
|
---|
70 | .de b
|
---|
71 | .TP 0.5i
|
---|
72 | \fI\\$1\fB \\$2 \fI\\$3\fR \-> \\fI\\$4\\fR (\\np)
|
---|
73 | ..
|
---|
74 | .de u
|
---|
75 | .TP 0.5i
|
---|
76 | \\$1 \fI\\$2\fR \-> \\fI\\$3\\fR (\\np)
|
---|
77 | ..
|
---|
78 | .b any \-o any any
|
---|
79 | Returns the value of the left hand operand if the left hand operand
|
---|
80 | would yield
|
---|
81 | .B true
|
---|
82 | if converted to type
|
---|
83 | .BR boolean ,
|
---|
84 | and the value of the right hand operand otherwise.
|
---|
85 | The right hand operand is evaluated only if necessary.
|
---|
86 | ``|'' is a synonym for ``\-o''.
|
---|
87 | .nr p \np+1
|
---|
88 | .b any -a any any
|
---|
89 | Returns the value of the left hand operand if the left hand operand
|
---|
90 | would yield
|
---|
91 | .B false
|
---|
92 | if converted to type
|
---|
93 | .BR boolean ,
|
---|
94 | and the value of the right hand operand otherwise.
|
---|
95 | The right hand operand is evaluated only if necessary.
|
---|
96 | ``&'' is a synonym for ``\-a''.
|
---|
97 | .nr p \np+1
|
---|
98 | .u ! boolean boolean
|
---|
99 | Returns true if the operand is false, and false if the operand is true.
|
---|
100 | .nr p \np+1
|
---|
101 | .b string = string boolean
|
---|
102 | True if the two strings are equal.
|
---|
103 | .b string != string boolean
|
---|
104 | True if the two strings are not equal.
|
---|
105 | .b integer \-eq integer boolean
|
---|
106 | True if the two operands are equal.
|
---|
107 | .b integer \-ne integer boolean
|
---|
108 | True if the two operands are not equal.
|
---|
109 | .b integer \-gt integer boolean
|
---|
110 | True if the first operand is greater than the second one.
|
---|
111 | .b integer \-lt integer boolean
|
---|
112 | True if the first operand is less than the second one.
|
---|
113 | .b integer \-ge integer boolean
|
---|
114 | True if the first operand is greater than or equal to the second one.
|
---|
115 | .b integer \-le integer boolean
|
---|
116 | True if the first operand is less than or equal to the second one.
|
---|
117 | .nr p \np+1
|
---|
118 | .b integer + integer integer
|
---|
119 | Add two integers.
|
---|
120 | .b integer \- integer integer
|
---|
121 | Subtract two integers.
|
---|
122 | .nr p \np+1
|
---|
123 | .b integer * integer integer
|
---|
124 | Multiply two integers. ``*'' is special to the shell, so you generally
|
---|
125 | have to write this operator as ``\e*''.
|
---|
126 | .b integer / integer integer
|
---|
127 | Divide two integers.
|
---|
128 | .b integer % integer integer
|
---|
129 | Returns the remainder when the first operand is divided by the second one.
|
---|
130 | .nr p \np+1
|
---|
131 | .b string : string "integer or string"
|
---|
132 | The second operand is interpreted as a regular expression (as in the
|
---|
133 | System V
|
---|
134 | .B ed
|
---|
135 | program).
|
---|
136 | This operator attempts to match part (or all) of the first operand
|
---|
137 | with the regular expression. The match must start at the beginning of
|
---|
138 | the first operand.
|
---|
139 | If the regular expression contains \e( \e) pairs, then the result
|
---|
140 | of this operator is the string which is matched by the regular expression
|
---|
141 | between these pairs, or the null string if no match occurred. Otherwise,
|
---|
142 | the result is the number of characters matched by the regular expression,
|
---|
143 | or zero if no no match occurred.
|
---|
144 | .nr p \np+1
|
---|
145 | .u \-n string integer
|
---|
146 | Returns the number of characters in the string.
|
---|
147 | .u \-z string boolean
|
---|
148 | Returns true if the string contains zero characters.
|
---|
149 | .u \-t integer boolean
|
---|
150 | Returns true if the specified file descriptor is associated with a tty.
|
---|
151 | .PP
|
---|
152 | The remaining operators all deal with files. Except as noted, they return
|
---|
153 | false if the
|
---|
154 | specified file does not exist. The ones dealing with permission use
|
---|
155 | the effective user and group ids of the shell.
|
---|
156 | .u \-r string boolean
|
---|
157 | True if you have read permission on the file.
|
---|
158 | .u \-w string boolean
|
---|
159 | True if you have write permission on the file.
|
---|
160 | .u \-x string boolean
|
---|
161 | True if you have execute permission on the file.
|
---|
162 | .u \-f string boolean
|
---|
163 | True if the file is a regular file.
|
---|
164 | .u \-d string boolean
|
---|
165 | True if the file is a directory.
|
---|
166 | .u \-c string boolean
|
---|
167 | True if the file is a character special file.
|
---|
168 | .u \-b string boolean
|
---|
169 | True if the file is a block special file.
|
---|
170 | .u \-p string boolean
|
---|
171 | True if the file is a named pipe (i.e. a fifo).
|
---|
172 | .u \-u string boolean
|
---|
173 | True if the file is setuid.
|
---|
174 | .u \-g string boolean
|
---|
175 | True if the file is setgid.
|
---|
176 | .u \-k string boolean
|
---|
177 | True if the file has the sticky bit set.
|
---|
178 | .u \-s string "integer or boolean"
|
---|
179 | Returns the size of the file, or 0 if the file does not exist.
|
---|
180 | .u \-h string boolean
|
---|
181 | True if the file is a symlink. This is the only file test operator that
|
---|
182 | does not follow symlinks, all others do. So ``\-d'' and ``\-h''
|
---|
183 | are both true on a symlink pointing to a directory.
|
---|
184 | ``\-L'' is a synonym for ``\-h''.
|
---|
185 | .SH "EXIT CODE"
|
---|
186 | 0 if the result of
|
---|
187 | .I expression
|
---|
188 | would be
|
---|
189 | .B true
|
---|
190 | if the result were converted to
|
---|
191 | .BR boolean .
|
---|
192 | .br
|
---|
193 | 1 if the result of
|
---|
194 | .I expression
|
---|
195 | would be
|
---|
196 | .B false
|
---|
197 | if the result were converted to
|
---|
198 | .BR boolean .
|
---|
199 | .br
|
---|
200 | 2 if
|
---|
201 | .I expression
|
---|
202 | is syntactically incorrect.
|
---|
203 | .SH EXAMPLES
|
---|
204 | .TP 0.5i
|
---|
205 | filesize=`expr \-s file`
|
---|
206 | Sets the shell variable
|
---|
207 | .I filesize
|
---|
208 | to the size of
|
---|
209 | .IR file .
|
---|
210 | .TP 0.5i
|
---|
211 | if [ \-s file ]; then command; fi
|
---|
212 | Execute
|
---|
213 | .I command
|
---|
214 | if
|
---|
215 | .I file
|
---|
216 | exists and is not empty.
|
---|
217 | .TP 0.5i
|
---|
218 | x=`expr "$x" : '.\\{4\\}\\(.\\{0,3\\}\\)'`
|
---|
219 | Sets
|
---|
220 | .I x
|
---|
221 | to the substring of
|
---|
222 | .I x
|
---|
223 | beginning after the fourth character of
|
---|
224 | .I x
|
---|
225 | and continuing for three characters or until the end of the string,
|
---|
226 | whichever comes first.
|
---|
227 | .TP 0.5i
|
---|
228 | x=`expr X"$x" : X'.\\{4\\}\\(.\\{0,3\\}\\)'`
|
---|
229 | This example is the same as the previous one, but it uses a leading
|
---|
230 | ``X'' to make things work when the value of
|
---|
231 | .I x
|
---|
232 | looks like an operator.
|
---|
233 | .SH BUGS
|
---|
234 | The relational operators of the System V
|
---|
235 | .B expr
|
---|
236 | command are not implemented.
|
---|
237 | .PP
|
---|
238 | Certain features of this version of
|
---|
239 | .B expr
|
---|
240 | are not present in System V, so care should be used when writing
|
---|
241 | portable code.
|
---|
242 | .SH COPYRIGHT
|
---|
243 | Kenneth Almquist.
|
---|