Test (Unix)
test is a command-line utility found in Unix, Plan 9, and Unix-like operating systems that evaluates conditional expressions. test was turned into a shell builtin command in 1981 with UNIX System III and at the same time made available under the alternate name [.[1] OverviewThe The The version of Syntax
ArgumentsThe following arguments are used to construct this parameter. All arguments return
For the Non standard Korn Shell extensionsfile1 -nt file2 - file1 is newer than file2 file1 -ot file2 - file1 is older than file2 file1 -ef file2 - file1 is another name for file2 - (symbolic link or hard link) String argumentsIn Perl, these sections are reversed: -n String1 - the length of the String1 variable is nonzero -z String1 - the length of the String1 variable is 0 (zero) String1 = String2 - String1 and String2 variables are identical String1 != String2 - String1 and String2 variables are not identical String1 - true if String1 variable is not a null string Number argumentsInteger1 -eq Integer2 - Integer1 and Integer2 variables are algebraically equal -ne - not equal -gt - greater than -ge - greater or equal -lt - less than -le - less or equal Operators
! - Unary negation operator -a - Binary AND operator -o - Binary OR operator (the The Exit statusThis command returns the following exit values: 0 - The Expression parameter is true 1 - The Expression parameter is false or missing >1 - An error occurred Examples1. To test whether a file is nonexistent or empty, type: if test ! -s "$1"
then
echo $1 does not exist or is empty.
fi
If the file specified by the first positional parameter to the shell procedure, $1, does not exist or is of size 0, the test command displays the message. If $1 exists and has a size greater than 0, the test command displays nothing. Note: There must be a space between the -s function and the file name. The quotation marks around $1 ensure that the test works properly even if the value of $1 is a null string. If the quotation marks are omitted and $1 is the empty string, the test command displays the error message: test: argument expected. 2. To do a complex comparison, type: if [ "$#" -lt 2 ] || ! [ -e "$1" ]
then
exit
fi
If the shell procedure is given fewer than two positional parameters or the file specified by $1 does not exist, then the shell procedure exits. The special shell variable $# represents the number of positional parameters entered on the command line that starts this shell procedure. See alsoReferences
Further reading
External linksThe Wikibook Guide to Unix has a page on the topic of: Commands
|
Portal di Ensiklopedia Dunia