2012-07-03 21:54:00 +00:00
|
|
|
\section test test - perform tests on files and text
|
|
|
|
|
|
|
|
\subsection test-synopsis Synopsis
|
2014-08-01 12:25:41 +00:00
|
|
|
\fish{synopsis}
|
2014-08-01 02:37:32 +00:00
|
|
|
test [EXPRESSION]
|
|
|
|
\endfish
|
2012-07-03 21:54:00 +00:00
|
|
|
|
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 07:56:01 +00:00
|
|
|
\subsection test-description Description
|
2012-07-03 21:54:00 +00:00
|
|
|
|
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 07:56:01 +00:00
|
|
|
Tests the expression given and sets the exit status to 0 if true,
|
2013-07-29 08:06:05 +00:00
|
|
|
and 1 if false. An expression is made up of one or more operators
|
|
|
|
and their arguments.
|
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 07:56:01 +00:00
|
|
|
|
2013-07-29 08:06:05 +00:00
|
|
|
The following operators are available to examine files and directories:
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-b FILE` returns true if `FILE` is a block device.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-c FILE` returns true if `FILE` is a character device.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-d FILE` returns true if `FILE` is a directory.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-e FILE` returns true if `FILE` exists.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-f FILE` returns true if `FILE` is a regular file.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-g FILE` returns true if `FILE` has the set-group-ID bit set.
|
|
|
|
- `-G FILE` returns true if `FILE` exists and has the same group ID
|
2013-09-09 12:01:38 +00:00
|
|
|
as the current user.
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-L FILE` returns true if `FILE` is a symbolic link.
|
|
|
|
- `-O FILE` returns true if `FILE` exists and is owned by the current
|
2013-09-09 12:01:38 +00:00
|
|
|
user.
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-p FILE` returns true if `FILE` is a named pipe.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-r FILE` returns true if `FILE` is marked as readable.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-s FILE` returns true if the size of `FILE` is greater than zero.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-S FILE` returns true if `FILE` is a socket.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-t FD` returns true if the file descriptor `FD` is a terminal (TTY).
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-u FILE` returns true if `FILE` has the set-user-ID bit set.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-w FILE` returns true if `FILE` is marked as writable; note that this does not check if the filesystem is read-only.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-x FILE` returns true if `FILE` is marked as executable.
|
2013-07-29 08:06:05 +00:00
|
|
|
|
|
|
|
The following operators are available to compare and examine text strings:
|
2014-08-01 02:37:32 +00:00
|
|
|
- `STRING1 = STRING2` returns true if the strings `STRING1` and
|
|
|
|
`STRING2` are identical.
|
|
|
|
- `STRING1 != STRING2` returns true if the strings `STRING1` and
|
|
|
|
`STRING2` are not identical.
|
|
|
|
- `-n STRING` returns true if the length of `STRING` is non-zero.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `-z STRING` returns true if the length of `STRING` is zero.
|
2013-07-29 08:06:05 +00:00
|
|
|
|
|
|
|
The following operators are available to compare and examine numbers:
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -eq NUM2` returns true if `NUM1` and `NUM2` are numerically equal.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -ne NUM2` returns true if `NUM1` and `NUM2` are not numerically equal.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -gt NUM2` returns true if `NUM1` is greater than `NUM2`.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -ge NUM2` returns true if `NUM1` is greater than or equal to `NUM2`.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -lt NUM2` returns true if `NUM1` is less than `NUM2`.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `NUM1 -le NUM2` returns true if `NUM1` is less than or equal to `NUM2`.
|
2013-07-29 08:06:05 +00:00
|
|
|
|
|
|
|
Note that only integers are supported. For more complex mathematical
|
2014-08-01 02:37:32 +00:00
|
|
|
operations, including fractions, the `env` program may be useful. Consult the
|
2013-07-29 08:06:05 +00:00
|
|
|
documentation for your operating system.
|
|
|
|
|
|
|
|
Expressions can be combined using the following operators:
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `COND1 -a COND2` returns true if both `COND1` and `COND2` are true.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `COND1 -o COND2` returns true if either `COND1` or `COND2` are true.
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
Expressions can be inverted using the `!` operator:
|
|
|
|
- `! EXPRESSION` returns true if `EXPRESSION` is false, and false if
|
|
|
|
`EXPRESSION` is true.
|
2012-07-03 21:54:00 +00:00
|
|
|
|
2013-07-29 08:06:05 +00:00
|
|
|
Expressions can be grouped using parentheses.
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
- `( EXPRESSION )` returns the value of `EXPRESSION`.
|
|
|
|
Note that parentheses will usually require escaping with `\\(` to avoid
|
2013-07-29 08:06:05 +00:00
|
|
|
being interpreted as a command substitution.
|
|
|
|
|
|
|
|
\subsection test-example Examples
|
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
If the `/tmp` directory exists, copy the `/etc/motd` file to it:
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
\fish
|
2013-07-29 08:06:05 +00:00
|
|
|
if test -d /tmp
|
|
|
|
cp /etc/motd /tmp/motd
|
|
|
|
end
|
2014-08-01 02:37:32 +00:00
|
|
|
\endfish
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-07-29 21:44:51 +00:00
|
|
|
If the variable \c MANPATH is defined and not empty, print the contents.
|
|
|
|
(If \c MANPATH is not defined, then it will expand to zero arguments, unless
|
|
|
|
quoted.)
|
2012-07-03 21:54:00 +00:00
|
|
|
|
2012-11-05 01:38:42 +00:00
|
|
|
<pre>
|
2014-07-29 21:44:51 +00:00
|
|
|
if test -n "$MANPATH"
|
2013-07-29 08:06:05 +00:00
|
|
|
echo $MANPATH
|
Help cleanup
Large list of changes, including formatting and typos for most commands.
More substantive changes have been made to alias, bind, block, break,
builtin, case, cd, commandline, count, else, emit, fish_config, funced,
function, functions, history, math, mimedb, nextd, not, popd, prevd,
pushd, pwd, random, read, set, set_color, switch, test, trap, type,
ulimit, umask, and while.
2013-05-12 07:56:01 +00:00
|
|
|
end
|
2014-08-01 02:37:32 +00:00
|
|
|
\endfish
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
Parentheses and the `-o` and `-a` operators can be combined to produce
|
2013-07-29 08:06:05 +00:00
|
|
|
more complicated expressions. In this example, success is printed if there is
|
2014-08-01 02:37:32 +00:00
|
|
|
a `/foo` or `/bar` file as well as a `/baz` or `/bat` file.
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
\fish
|
|
|
|
if test \( -f /foo -o -f /bar \) -a \( -f /baz -o -f /bat \)
|
2013-07-29 08:06:05 +00:00
|
|
|
echo Success.
|
|
|
|
end.
|
2014-08-01 02:37:32 +00:00
|
|
|
\endfish
|
2013-07-29 08:06:05 +00:00
|
|
|
|
2014-08-19 12:41:23 +00:00
|
|
|
|
2013-07-29 08:06:05 +00:00
|
|
|
\subsection test-standards Standards
|
|
|
|
|
2014-08-01 02:37:32 +00:00
|
|
|
`test` implements a subset of the
|
2013-07-29 08:06:05 +00:00
|
|
|
<a href="http://www.unix.com/man-page/POSIX/1/test/">IEEE Std 1003.1-2008
|
|
|
|
(POSIX.1) standard</a>. The following exceptions apply:
|
2014-08-01 02:37:32 +00:00
|
|
|
- The `<` and `>` operators for comparing strings are not implemented.
|
2014-01-08 03:57:49 +00:00
|
|
|
- Because this test is a shell builtin and not a standalone utility, using
|
|
|
|
the -c flag on a special file descriptors like standard input and output
|
|
|
|
may not return the same result when invoked from within a pipe as one
|
2014-08-01 02:37:32 +00:00
|
|
|
would expect when invoking the `test` utility in another shell.
|
|
|
|
|
|
|
|
In cases such as this, one can use `command` `test` to explicitly
|
|
|
|
use the system's standalone `test` rather than this `builtin` `test`.
|
2012-07-03 21:54:00 +00:00
|
|
|
|