Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is test.def, from which is created test.c. |
| 2 | It implements the builtin "test" in Bash. |
| 3 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2010 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES test.c |
| 22 | |
| 23 | $BUILTIN test |
| 24 | $FUNCTION test_builtin |
| 25 | $SHORT_DOC test [expr] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Evaluate conditional expression. |
| 27 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 28 | Exits with a status of 0 (true) or 1 (false) depending on |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 29 | the evaluation of EXPR. Expressions may be unary or binary. Unary |
| 30 | expressions are often used to examine the status of a file. There |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 31 | are string operators and numeric comparison operators as well. |
| 32 | |
| 33 | The behavior of test depends on the number of arguments. Read the |
| 34 | bash manual page for the complete specification. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 35 | |
| 36 | File operators: |
| 37 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 38 | -a FILE True if file exists. |
| 39 | -b FILE True if file is block special. |
| 40 | -c FILE True if file is character special. |
| 41 | -d FILE True if file is a directory. |
| 42 | -e FILE True if file exists. |
| 43 | -f FILE True if file exists and is a regular file. |
| 44 | -g FILE True if file is set-group-id. |
| 45 | -h FILE True if file is a symbolic link. |
| 46 | -L FILE True if file is a symbolic link. |
| 47 | -k FILE True if file has its `sticky' bit set. |
| 48 | -p FILE True if file is a named pipe. |
| 49 | -r FILE True if file is readable by you. |
| 50 | -s FILE True if file exists and is not empty. |
| 51 | -S FILE True if file is a socket. |
| 52 | -t FD True if FD is opened on a terminal. |
| 53 | -u FILE True if the file is set-user-id. |
| 54 | -w FILE True if the file is writable by you. |
| 55 | -x FILE True if the file is executable by you. |
| 56 | -O FILE True if the file is effectively owned by you. |
| 57 | -G FILE True if the file is effectively owned by your group. |
| 58 | -N FILE True if the file has been modified since it was last read. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 59 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 60 | FILE1 -nt FILE2 True if file1 is newer than file2 (according to |
| 61 | modification date). |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 62 | |
| 63 | FILE1 -ot FILE2 True if file1 is older than file2. |
| 64 | |
| 65 | FILE1 -ef FILE2 True if file1 is a hard link to file2. |
| 66 | |
| 67 | String operators: |
| 68 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 69 | -z STRING True if string is empty. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 70 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 71 | -n STRING |
| 72 | STRING True if string is not empty. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 74 | STRING1 = STRING2 |
| 75 | True if the strings are equal. |
| 76 | STRING1 != STRING2 |
| 77 | True if the strings are not equal. |
| 78 | STRING1 < STRING2 |
| 79 | True if STRING1 sorts before STRING2 lexicographically. |
| 80 | STRING1 > STRING2 |
| 81 | True if STRING1 sorts after STRING2 lexicographically. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 82 | |
| 83 | Other operators: |
| 84 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 85 | -o OPTION True if the shell option OPTION is enabled. |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 86 | -v VAR True if the shell variable VAR is set |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 87 | -R VAR True if the shell variable VAR is set and is a name reference. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 88 | ! EXPR True if expr is false. |
| 89 | EXPR1 -a EXPR2 True if both expr1 AND expr2 are true. |
| 90 | EXPR1 -o EXPR2 True if either expr1 OR expr2 is true. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 92 | arg1 OP arg2 Arithmetic tests. OP is one of -eq, -ne, |
| 93 | -lt, -le, -gt, or -ge. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 94 | |
| 95 | Arithmetic binary operators return true if ARG1 is equal, not-equal, |
| 96 | less-than, less-than-or-equal, greater-than, or greater-than-or-equal |
| 97 | than ARG2. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 98 | |
| 99 | Exit Status: |
| 100 | Returns success if EXPR evaluates to true; fails if EXPR evaluates to |
| 101 | false or an invalid argument is given. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | $END |
| 103 | |
| 104 | $BUILTIN [ |
| 105 | $DOCNAME test_bracket |
| 106 | $FUNCTION test_builtin |
| 107 | $SHORT_DOC [ arg... ] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 108 | Evaluate conditional expression. |
| 109 | |
| 110 | This is a synonym for the "test" builtin, but the last argument must |
| 111 | be a literal `]', to match the opening `['. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 112 | $END |
| 113 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 114 | #include <config.h> |
| 115 | |
| 116 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 117 | # ifdef _MINIX |
| 118 | # include <sys/types.h> |
| 119 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 120 | # include <unistd.h> |
| 121 | #endif |
| 122 | |
| 123 | #include "../bashansi.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 124 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 125 | |
| 126 | #include "../shell.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 127 | #include "../test.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 128 | #include "common.h" |
| 129 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 130 | extern char *this_command_name; |
| 131 | |
| 132 | /* TEST/[ builtin. */ |
| 133 | int |
| 134 | test_builtin (list) |
| 135 | WORD_LIST *list; |
| 136 | { |
| 137 | char **argv; |
| 138 | int argc, result; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 139 | |
| 140 | /* We let Matthew Bradburn and Kevin Braunsdorf's code do the |
| 141 | actual test command. So turn the list of args into an array |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 142 | of strings, since that is what their code wants. */ |
| 143 | if (list == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 144 | { |
| 145 | if (this_command_name[0] == '[' && !this_command_name[1]) |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 146 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 147 | builtin_error (_("missing `]'")); |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 148 | return (EX_BADUSAGE); |
| 149 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 150 | |
| 151 | return (EXECUTION_FAILURE); |
| 152 | } |
| 153 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 154 | argv = make_builtin_argv (list, &argc); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 155 | result = test_command (argc, argv); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 156 | free ((char *)argv); |
| 157 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 158 | return (result); |
| 159 | } |