expr.1
来自「minix操作系统最新版本(3.1.1)的源代码」· 1 代码 · 共 244 行
1
244 行
.TH EXPR 1.SH NAME \" Copyright (C) 1989 by Kenneth Almquist.expr, test, [ \- evaluate expressions.SH SYNOPSIS.B expr.I expression.br.B test.I expression.br.B [.I expression.B ].SH DESCRIPTION.B Exprevaluates the expression and prints the result..B Testevaluates the expression without printing the result.The ``[''command is a synonym for.BR test ;when invoked under this namethe last argument to.B exprmust be a ``]'', which is deleted and not considered part of the expression..PPThree data types may occur in the.IR expression :string, integer, and boolean.The rules for conversion are as follows:.sp.nr i 2.ta \nii.in +\nii.ti -\nii\fIstring\fR\->\fIinteger\fR Done via.BR atoi (3)..ti -\nii\fIinteger\fR\->\fIstring\fR Convert to decimal representation..ti -\nii\fIstring\fR\->\fIboolean\fR "" \-> false, everything else to true..ti -\nii\fIboolean\fR\->\fIstring\fR false \-> "", true \-> "true"..ti -\nii\fIinteger\fR\->\fIboolean\fR 0 \-> false, everything else to true..ti -\nii\fIboolean\fR\->\fIinteger\fR false \-> 0, true \-> 1..in -\nii.PPAny argument to.B exprwhich is not a legal operator is treated as a string operand of type.BR string ..PPAs a special case, if.I expressionis omitted, the result is false..PPWe now list the operators. The syntax.sp.ti +8\fIinteger\fB op \fIinteger\fR \-> \fIboolean\fB (3)\fR.spmeans that \fBop\fR is a binary operator which takes operands of type\fIinteger\fR and produces a result of type \fIboolean\fR.The ``(3)'' means that the priority of \fBop\fR is 3.Operands are automatically converted to the appropriate type. The type\fIany\fR is used for operator that take operands of any type..nr p 1.de b.TP 0.5i\fI\\$1\fB \\$2 \fI\\$3\fR \-> \\fI\\$4\\fR (\\np)...de u.TP 0.5i\\$1 \fI\\$2\fR \-> \\fI\\$3\\fR (\\np)...b any \-o any anyReturns the value of the left hand operand if the left hand operandwould yield.B trueif converted to type.BR boolean ,and the value of the right hand operand otherwise.The right hand operand is evaluated only if necessary.``|'' is a synonym for ``\-o''..nr p \np+1.b any -a any anyReturns the value of the left hand operand if the left hand operandwould yield.B falseif converted to type.BR boolean ,and the value of the right hand operand otherwise.The right hand operand is evaluated only if necessary.``&'' is a synonym for ``\-a''..nr p \np+1.u ! boolean booleanReturns true if the operand is false, and false if the operand is true..nr p \np+1.b string = string booleanTrue if the two strings are equal..b string != string booleanTrue if the two strings are not equal..b integer \-eq integer booleanTrue if the two operands are equal..b integer \-ne integer booleanTrue if the two operands are not equal..b integer \-gt integer booleanTrue if the first operand is greater than the second one..b integer \-lt integer booleanTrue if the first operand is less than the second one..b integer \-ge integer booleanTrue if the first operand is greater than or equal to the second one..b integer \-le integer booleanTrue if the first operand is less than or equal to the second one..nr p \np+1.b integer + integer integerAdd two integers..b integer \- integer integerSubtract two integers..nr p \np+1.b integer * integer integerMultiply two integers. ``*'' is special to the shell, so you generallyhave to write this operator as ``\e*''..b integer / integer integerDivide two integers..b integer % integer integerReturns the remainder when the first operand is divided by the second one..nr p \np+1.b string : string "integer or string"The second operand is interpreted as a regular expression (as in theSystem V.B edprogram).This operator attempts to match part (or all) of the first operandwith the regular expression. The match must start at the beginning ofthe first operand.If the regular expression contains \e( \e) pairs, then the resultof this operator is the string which is matched by the regular expressionbetween these pairs, or the null string if no match occurred. Otherwise,the result is the number of characters matched by the regular expression,or zero if no no match occurred..nr p \np+1.u \-n string integerReturns the number of characters in the string..u \-z string booleanReturns true if the string contains zero characters..u \-t integer booleanReturns true if the specified file descriptor is associated with a tty..PPThe remaining operators all deal with files. Except as noted, they returnfalse if thespecified file does not exist. The ones dealing with permission usethe effective user and group ids of the shell..u \-r string booleanTrue if you have read permission on the file..u \-w string booleanTrue if you have write permission on the file..u \-x string booleanTrue if you have execute permission on the file..u \-f string booleanTrue if the file is a regular file..u \-d string booleanTrue if the file is a directory..u \-c string booleanTrue if the file is a character special file..u \-b string booleanTrue if the file is a block special file..u \-p string booleanTrue if the file is a named pipe (i.e. a fifo)..u \-u string booleanTrue if the file is setuid..u \-g string booleanTrue if the file is setgid..u \-k string booleanTrue if the file has the sticky bit set..u \-s string "integer or boolean"Returns the size of the file, or 0 if the file does not exist..u \-h string booleanTrue if the file is a symlink. This is the only file test operator thatdoes not follow symlinks, all others do. So ``\-d'' and ``\-h''are both true on a symlink pointing to a directory.``\-L'' is a synonym for ``\-h''..SH "EXIT CODE"0 if the result of .I expressionwould be.B trueif the result were converted to.BR boolean ..br1 if the result of .I expressionwould be.B falseif the result were converted to.BR boolean ..br2 if.I expressionis syntactically incorrect..SH EXAMPLES.TP 0.5ifilesize=`expr \-s file`Sets the shell variable.I filesizeto the size of.IR file ..TP 0.5iif [ \-s file ]; then command; fiExecute.I commandif.I fileexists and is not empty..TP 0.5ix=`expr "$x" : '.\\{4\\}\\(.\\{0,3\\}\\)'`Sets.I xto the substring of.I xbeginning after the fourth character of.I xand continuing for three characters or until the end of the string,whichever comes first..TP 0.5ix=`expr X"$x" : X'.\\{4\\}\\(.\\{0,3\\}\\)'`This example is the same as the previous one, but it uses a leading``X'' to make things work when the value of.I xlooks like an operator..SH BUGSThe relational operators of the System V.B exprcommand are not implemented..PPCertain features of this version of.B exprare not present in System V, so care should be used when writingportable code..SH COPYRIGHTKenneth Almquist.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?