test 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. .IB s1 " = " s2
  54. True
  55. if the strings
  56. .I s1
  57. and
  58. .I s2
  59. are identical.
  60. .TP
  61. .IB s1 " != " s2
  62. True
  63. if the strings
  64. .I s1
  65. and
  66. .I s2
  67. are not identical.
  68. .TP
  69. s1
  70. True if
  71. .I s1
  72. is not the null string.
  73. (Deprecated.)
  74. .TP
  75. .BI -n " s1"
  76. True if the length of string
  77. .I s1
  78. is non-zero.
  79. .TP
  80. .BI -z " s1"
  81. True if the length of string
  82. .I s1
  83. is zero.
  84. .TP
  85. .IB n1 " -eq " n2
  86. True if the integers
  87. .I n1
  88. and
  89. .I n2
  90. are arithmetically equal.
  91. Any of the comparisons
  92. .BR -ne ,
  93. .BR -gt ,
  94. .BR -ge ,
  95. .BR -lt ,
  96. or
  97. .BR -le
  98. may be used in place of
  99. .BR -eq .
  100. The (nonstandard) construct
  101. .BI -l " string\f1,
  102. meaning the length of
  103. .IR string ,
  104. may be used in place of an integer.
  105. .TP
  106. .IB a " -nt " b
  107. True if file
  108. .I a
  109. is newer than (modified after) file
  110. .IR b .
  111. .TP
  112. .IB a " -ot " b
  113. True if file
  114. .I a
  115. is older than (modified before) file
  116. .IR b .
  117. .TP
  118. .IB f " -older " t
  119. True if file
  120. .I f
  121. is older than (modified before) time
  122. .IR t .
  123. If
  124. .I t
  125. is a integer followed by the letters
  126. .BR y (years),
  127. .BR M (months),
  128. .BR d (days),
  129. .BR h (hours),
  130. .BR m (minutes),
  131. or
  132. .BR s (seconds),
  133. it represents current time minus the specified time.
  134. If there is no letter, it represents seconds since
  135. epoch.
  136. You can also concatenate mixed units. For example,
  137. .B 3d12h
  138. means three days and twelve hours ago.
  139. .PD
  140. .PP
  141. These primaries may be combined with the
  142. following operators:
  143. .TP "\w'\fL( \fIexpr\fL )XX'u"
  144. .B !
  145. unary negation operator
  146. .PD0
  147. .TP
  148. .B -o
  149. binary
  150. .I or
  151. operator
  152. .TP
  153. .B -a
  154. binary
  155. .I and
  156. operator; higher precedence than
  157. .BR -o
  158. .TP
  159. .BI "( " expr " )"
  160. parentheses for grouping.
  161. .PD
  162. .PP
  163. The primitives
  164. .BR -b ,
  165. .BR -u ,
  166. .BR -g ,
  167. and
  168. .BR -s
  169. return false; they are recognized for compatibility with POSIX.
  170. .PP
  171. Notice that all the operators and flags are separate
  172. arguments to
  173. .IR test .
  174. Notice also that parentheses and equal signs are meaningful
  175. to
  176. .I rc
  177. and must be enclosed in quotes.
  178. .SH EXAMPLES
  179. .I Test
  180. is a dubious way to check for specific character strings:
  181. it uses a process to do what an
  182. .IR rc (1)
  183. match or switch statement can do.
  184. The first example is not only inefficient but wrong, because
  185. .I test
  186. understands the purported string
  187. .B \&"-c"
  188. as an option.
  189. .IP
  190. .EX
  191. if (test $1 '=' "-c") echo OK # wrong!
  192. .EE
  193. .LP
  194. A better way is
  195. .IP
  196. .EX
  197. if (~ $1 -c) echo OK
  198. .EE
  199. .PP
  200. Test whether
  201. .L abc
  202. is in the current directory.
  203. .IP
  204. .B test -f abc -o -d abc
  205. .SH SOURCE
  206. .B /sys/src/cmd/test.c
  207. .SH "SEE ALSO"
  208. .IR rc (1)