source: trunk/minix/man/man1/expr.1@ 11

Last change on this file since 11 was 9, checked in by Mattia Monga, 14 years ago

Minix 3.1.2a

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