218 lines · 3.6 KB · Text
RawDownload
.TH TEST 1.SH NAMEtest \- set status according to condition.SH SYNOPSIS.B test.I expr.SH DESCRIPTION.I Testevaluates the expression.IR expr .If the value is true the exit status is null; otherwise theexit status is non-null.If there are no arguments the exit status is non-null..PPThe following primitives are used to construct.IR expr ..TP "\w'\fIn1 \fL-eq \fIn2\fLXX'u".BI -r " file"True if the file exists (is accessible) and is readable..PD0.TP.BI -w " file"True if the file exists and is writable..TP.BI -x " file"True if the file exists and has execute permission..TP.BI -e " fileTrue if the file exists..TP.BI -f " file"True if the file exists and is a plain file..TP.BI -d " file"True if the file exists and is a directory..TP.BI -s " file"True if the file exists and has a size greater than zero..TP.BI -t " fildesTrue if the open file whose file descriptor number is.I fildes(1 by default)is the same file as.BR /dev/cons ..TP.BI -A " file"True if the file exists and is append-only..TP.BI -L " file"True if the file exists and is exclusive-use..TP.BI -T "file"True if the file exists and is temporary..TP.IB s1 " = " s2Trueif the strings.I s1and.I s2are identical..TP.IB s1 " != " s2Trueif the strings.I s1and.I s2are not identical..TPs1True if.I s1is not the null string.(Deprecated.).TP.BI -n " s1"True if the length of string.I s1is non-zero..TP.BI -z " s1"True if the length of string.I s1is zero..TP.IB n1 " -eq " n2True if the integers.I n1and.I n2are arithmetically equal.Any of the comparisons.BR -ne ,.BR -gt ,.BR -ge ,.BR -lt ,or.BR -lemay be used in place of.BR -eq .The (nonstandard) construct.BI -l " string\f1,meaning the length of.IR string ,may be used in place of an integer..TP.IB a " -nt " bTrue if file.I ais newer than (modified after) file.IR b ..TP.IB a " -ot " bTrue if file.I ais older than (modified before) file.IR b ..TP.IB f " -older " tTrue if file.I fis older than (modified before) time.IR t .If.I tis a integer followed by the letters.BR y (years),.BR M (months),.BR d (days),.BR h (hours),.BR m (minutes),or.BR s (seconds),it represents current time minus the specified time.If there is no letter, it represents seconds sinceepoch.You can also concatenate mixed units.  For example,.B 3d12hmeans three days and twelve hours ago..PD.PPThese primaries may be combined with thefollowing operators:.TP "\w'\fL( \fIexpr\fL )XX'u".B  !unary negation operator.PD0.TP.B  -obinary.I oroperator.TP.B  -abinary.I andoperator; higher precedence than.BR -o.TP.BI "( " expr " )"parentheses for grouping..PD.PPThe primitives.BR -b ,.BR -u ,.BR -g ,and.BR -sreturn false; they are recognized for compatibility with POSIX..PPNotice that all the operators and flags are separatearguments to.IR test .Notice also that parentheses and equal signs are meaningfulto.I rcand must be enclosed in quotes..SH EXAMPLES.I Testis a dubious way to check for specific character strings:it uses a process to do what an.IR rc (1)match or switch statement can do.The first example is not only inefficient but wrong, because.I testunderstands the purported string.B  \&"-c"as an option..IP.EXif (test $1 '=' "-c") echo OK	# wrong!.EE.LPA better way is.IP.EXif (~ $1 -c) echo OK.EE.PPTest whether .L abcis in the current directory..IP.B test -f abc -o -d abc.SH SOURCE.B /sys/src/cmd/test.c.SH "SEE ALSO".IR rc (1) .SH BUGSWon't complain about extraneous argumentssince there may be arguments left unprocessed byshort-circuit evaluation of.B -aor.BR -o .