test 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. .PD
  106. .PP
  107. These primaries may be combined with the
  108. following operators:
  109. .TP "\w'\fL( \fIexpr\fL )XX'u"
  110. .B !
  111. unary negation operator
  112. .PD0
  113. .TP
  114. .B -o
  115. binary
  116. .I or
  117. operator
  118. .TP
  119. .B -a
  120. binary
  121. .I and
  122. operator; higher precedence than
  123. .BR -o
  124. .TP
  125. .BI "( " expr " )"
  126. parentheses for grouping.
  127. .PD
  128. .PP
  129. The primitives
  130. .BR -b ,
  131. .BR -u ,
  132. .BR -g ,
  133. and
  134. .BR -s
  135. return false; they are recognized for compatibility with POSIX.
  136. .PP
  137. Notice that all the operators and flags are separate
  138. arguments to
  139. .IR test .
  140. Notice also that parentheses and equal signs are meaningful
  141. to
  142. .I rc
  143. and must be enclosed in quotes.
  144. .SH EXAMPLES
  145. .I Test
  146. is a dubious way to check for specific character strings:
  147. it uses a process to do what an
  148. .IR rc (1)
  149. match or switch statement can do.
  150. The first example is not only inefficient but wrong, because
  151. .I test
  152. understands the purported string
  153. .B \&"-c"
  154. as an option.
  155. .IP
  156. .EX
  157. if (test $1 '=' "-c") echo OK # wrong!
  158. .EE
  159. .LP
  160. A better way is
  161. .IP
  162. .EX
  163. if (~ $1 -c) echo OK
  164. .EE
  165. .PP
  166. Test whether
  167. .L abc
  168. is in the current directory.
  169. .IP
  170. .B test -f abc -o -d abc
  171. .SH SOURCE
  172. .B /sys/src/cmd/test.c
  173. .SH "SEE ALSO"
  174. .IR rc (1)