Charles.Forsyth20061220965a0ca19 years agoHistory
160 lines · 3.1 KB · Text
RawDownload
.TH SH-EXPR 1.SH NAMEexpr, ntest \- shell module for simple arithmetic..SH SYNOPSIS.B load expr .B ${expr[-r.I radix][.I arg...].B }.br.B ntest.I num.br.SH DESCRIPTION.I Expris a loadable module for.IR sh (1)that provides support for simple integer arithmetic.It provides one command,.IR ntest ,which performs a simple boolean teston its integer argument, and thesubstitution operator.IR expr ,which takes an expression in Reverse Polishnotation, and yields its result..PP.I Ntestreturns true if its argument.I numis non-zero,and false otherwise..PP.I Exprevaluates each.I argin turn; if it is an integer it gets pushed ontothe stack; otherwise it should nameone of the operators below, whereuponthe appropriate number of operands arepopped off the stack, evaluated as argumentsto the operator, and the result pushed back ontothe stack. Arguments are passed to the operatorfirst-pushed first, so, for instance,.B ${expr 2 1 -}yields 1, not -1.Alternative names are given for some operators;this is to avoid the necessity of quoting operatorsthat contain.IR sh (1)metacharacters. All operations use 64-bit signedintegers; integers are given in the same form acceptableto Limbo. The relational operators yield either1 (true) or 0 (false). If the.B -roption is given,.I radixspecifies an output base for numbers yielded by.IR expr .Numbers are printed in a form suitable for re-interpretationby.IR expr ..PPWhen all its arguments have been evaluated,.B expryields all the values remaining on its stack, first pushedfirst. The operators supported by expr are as follows (the numberof operands required in is given parentheses):.TP 15.BR + \ (2)Addition.TP.BR - \ (2)Subtraction.TP.BR x ,\  * \ (2)Multiplication.TP.BR / \ (2)Division. Division by zero raises a.B divide by zeroexception..TP.BR % \ (2)Modulus. A zero modulus will cause a.B divide by zeroexception to be raised..TP.BR and \ (2)Bitwise-and..TP.BR or \ (2)Bitwise-or..TP.BR xor \ (2)Bitwise-xor..TP.BR ~ \ (1)Bitwise-complement...TP.BR _ \ (1)Unary minus..TP.BR << ,\  shl \ (2)Shift left..TP.BR >> ,\  shr \ (2)Shift right..TP.BR = ", " == ", " eq " (2)"Equality..TP.BR != ", " neq " (2)"Inequality..TP.BR > ", " gt " (2)"Greater than..TP.BR < ", " lt " (2)"Less than..TP.BR <= ", " le " (2)"Less than or equal to..TP.BR >= ", " ge " (2)"Greater than or equal to..TP.BR ! ", " not " (1)"Logical negation..TP .BI rep \ \f1(\fPn\f1)\fP.B Reprepeats the last operation (which musthave been a two-operand operation otherthan.BR seq )until the values in the stack are exhausted..TP.BR seq \ (2).B Seqpushes on the stack a sequence of numbers rangingnumerically from its first argument up to and includingits second argument. If its second argument isless than its first, the sequence will descend..SH SOURCE.B /appl/cmd/sh/expr.b.SH SEE ALSO.IR sh (1),.IR sh-std (1),.IR sh-tk (1).SH BUGSPostfix notation can be confusing.Any operators that contain shell metacharacters (e.g. ``*'', ``>'')must be quoted to avoid interpretation by the shell.