test 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. .TH TEST 1
  2. .SH NAME
  3. test \- set status according to condition
  4. .SH SYNOPSIS
  5. .B test
  6. .I expr
  7. .SH DESCRIPTION
  8. .I Test
  9. evaluates the expression
  10. .IR expr .
  11. If the value is true the exit status is null; otherwise the
  12. exit status is non-null.
  13. If there are no arguments the exit status is non-null.
  14. .PP
  15. The following primitives are used to construct
  16. .IR expr .
  17. .TP "\w'\fIn1 \fL-eq \fIn2\fLXX'u"
  18. .BI -r " file"
  19. True if the file exists (is accessible) and is readable.
  20. .PD0
  21. .TP
  22. .BI -w " file"
  23. True if the file exists and is writable.
  24. .TP
  25. .BI -x " file"
  26. True if the file exists and has execute permission.
  27. .TP
  28. .BI -e " file
  29. True if the file exists.
  30. .TP
  31. .BI -f " file"
  32. True if the file exists and is a plain file.
  33. .TP
  34. .BI -d " file"
  35. True if the file exists and is a directory.
  36. .TP
  37. .BI -s " file"
  38. True if the file exists and has a size greater than zero.
  39. .TP
  40. .BI -t " fildes
  41. True if the open file whose file descriptor number is
  42. .I fildes
  43. (1 by default)
  44. is the same file as
  45. .BR /dev/cons .
  46. .TP
  47. .BI -A " file"
  48. True if the file exists and is append-only.
  49. .TP
  50. .BI -L " file"
  51. True if the file exists and is exclusive-use.
  52. .TP
  53. .BI -T "file"
  54. True if the file exists and is temporary.
  55. .TP
  56. .IB s1 " = " s2
  57. True
  58. if the strings
  59. .I s1
  60. and
  61. .I s2
  62. are identical.
  63. .TP
  64. .IB s1 " != " s2
  65. True
  66. if the strings
  67. .I s1
  68. and
  69. .I s2
  70. are not identical.
  71. .TP
  72. s1
  73. True if
  74. .I s1
  75. is not the null string.
  76. (Deprecated.)
  77. .TP
  78. .BI -n " s1"
  79. True if the length of string
  80. .I s1
  81. is non-zero.
  82. .TP
  83. .BI -z " s1"
  84. True if the length of string
  85. .I s1
  86. is zero.
  87. .TP
  88. .IB n1 " -eq " n2
  89. True if the integers
  90. .I n1
  91. and
  92. .I n2
  93. are arithmetically equal.
  94. Any of the comparisons
  95. .BR -ne ,
  96. .BR -gt ,
  97. .BR -ge ,
  98. .BR -lt ,
  99. or
  100. .BR -le
  101. may be used in place of
  102. .BR -eq .
  103. The (nonstandard) construct
  104. .BI -l " string\f1,
  105. meaning the length of
  106. .IR string ,
  107. may be used in place of an integer.
  108. .TP
  109. .IB a " -nt " b
  110. True if file
  111. .I a
  112. is newer than (modified after) file
  113. .IR b .
  114. .TP
  115. .IB a " -ot " b
  116. True if file
  117. .I a
  118. is older than (modified before) file
  119. .IR b .
  120. .TP
  121. .IB f " -older " t
  122. True if file
  123. .I f
  124. is older than (modified before) time
  125. .IR t .
  126. If
  127. .I t
  128. is a integer followed by the letters
  129. .BR y (years),
  130. .BR M (months),
  131. .BR d (days),
  132. .BR h (hours),
  133. .BR m (minutes),
  134. or
  135. .BR s (seconds),
  136. it represents current time minus the specified time.
  137. If there is no letter, it represents seconds since
  138. epoch.
  139. You can also concatenate mixed units. For example,
  140. .B 3d12h
  141. means three days and twelve hours ago.
  142. .PD
  143. .PP
  144. These primaries may be combined with the
  145. following operators:
  146. .TP "\w'\fL( \fIexpr\fL )XX'u"
  147. .B !
  148. unary negation operator
  149. .PD0
  150. .TP
  151. .B -o
  152. binary
  153. .I or
  154. operator
  155. .TP
  156. .B -a
  157. binary
  158. .I and
  159. operator; higher precedence than
  160. .BR -o
  161. .TP
  162. .BI "( " expr " )"
  163. parentheses for grouping.
  164. .PD
  165. .PP
  166. The primitives
  167. .BR -b ,
  168. .BR -u ,
  169. .BR -g ,
  170. and
  171. .BR -s
  172. return false; they are recognized for compatibility with POSIX.
  173. .PP
  174. Notice that all the operators and flags are separate
  175. arguments to
  176. .IR test .
  177. Notice also that parentheses and equal signs are meaningful
  178. to
  179. .I rc
  180. and must be enclosed in quotes.
  181. .SH EXAMPLES
  182. .I Test
  183. is a dubious way to check for specific character strings:
  184. it uses a process to do what an
  185. .IR rc (1)
  186. match or switch statement can do.
  187. The first example is not only inefficient but wrong, because
  188. .I test
  189. understands the purported string
  190. .B \&"-c"
  191. as an option.
  192. .IP
  193. .EX
  194. if (test $1 '=' "-c") echo OK # wrong!
  195. .EE
  196. .LP
  197. A better way is
  198. .IP
  199. .EX
  200. if (~ $1 -c) echo OK
  201. .EE
  202. .PP
  203. Test whether
  204. .L abc
  205. is in the current directory.
  206. .IP
  207. .B test -f abc -o -d abc
  208. .SH SOURCE
  209. .B /sys/src/cmd/test.c
  210. .SH "SEE ALSO"
  211. .IR rc (1)