To install click the Add extension button. That's it.

The source code for the WIKI 2 extension is being checked by specialists of the Mozilla Foundation, Google, and Apple. You could also do it yourself at any point in time.

4,5
Kelly Slayton
Congratulations on this excellent venture… what a great idea!
Alexander Grigorievskiy
I use WIKI 2 every day and almost forgot how the original Wikipedia looks like.
Live Statistics
English Articles
Improved in 24 Hours
Added in 24 Hours
What we do. Every page goes through several hundred of perfecting techniques; in live mode. Quite the same Wikipedia. Just better.
.
Leo
Newton
Brights
Milds

From Wikipedia, the free encyclopedia

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]

YouTube Encyclopedic

  • 1/5
    Views:
    178 482
    1 328
    40 241
    49 032
    5 416 906
  • Part 1 - Unix/Linux for Testers | File Commands
  • Testing A Unix Utility - Software Testing
  • Part 15 - Unix/Linux for Testers | vi Editor | Putty & WinSCP
  • Part 2 - Unix/Linux for Testers | Directory Commands
  • Linux for Ethical Hackers (Kali Linux Tutorial)

Transcription

Overview

The test command in Unix evaluates the expression parameter. In most recent shell implementations, it is a shell builtin, even though the external version still exists. In the second form of the command, the [ ] (brackets) must be surrounded by blank spaces (this is because [ is a program and POSIX compatible shells require a space between the program name and its arguments). One must test explicitly for file names in the C shell. File-name substitution (globbing) causes the shell script to exit.

The test command is not to be confused with the [[ reserved word that was introduced with ksh88. The latter is not a command but part of the ksh88 syntax and does not apply file-name substitution to glob expressions.

The version of test bundled in GNU coreutils was written by Kevin Braunsdorf and Matthew Bradburn.[2] The command is available as a separate package for Microsoft Windows as part of the UnxUtils collection of native Win32 ports of common GNU Unix-like utilities.[3] The test command has also been ported to the IBM i operating system.[4]

Syntax

test expression or [ expression ]

Arguments

The following arguments are used to construct this parameter. All arguments return True if the object (file or string) exists, and the condition specified is true.

Argument Returns True if the file
-b is a block special file
-c is a character special file
-d is a directory
-e exists
-f is a regular file
-g has the Set Group ID bit set
-h is a symbolic link
-k has the sticky bit set
-L is a symbolic link
-p is a named pipe (FIFO)
-r is readable by the current process
-s has a size greater than 0
-t FileDescriptor is open and associated with a terminal
-u has the Set User ID bit set
-w has the write flag is on
-x has execute flag on

For the -x argument, if the specified file exists and is a directory, the True exit value indicates that the current process has permission to change cd into the directory.

Non standard Korn Shell extensions

file1 -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 arguments

In Perl, these sections are reversed: eq is a string operator and == is a numerical operator, and so on for the others.

-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 arguments

Integer1 -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

test arguments can be combined with the following operators:

! - Unary negation operator
-a - Binary AND operator
-o - Binary OR operator (the -a operator has higher precedence than the -o operator)
\(Expression\) - Parentheses for grouping must be escaped with a backslash \

The -a and -o operators, along with parentheses for grouping, are XSI extensions[5] and are therefore not portable. In portable shell scripts, the same effect may be achieved by connecting multiple invocations of test together with the && and || operators and parentheses.

Exit status

This command returns the following exit values:

0 - The Expression parameter is true
1 - The Expression parameter is false or missing
>1 - An error occurred

Examples

1. 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 also

References

  1. ^ http://www.in-ulm.de/~mascheck/bourne/#system3 Bourne Shell changes with System III
  2. ^ test(1) — coreutils — Debian buster — Debian Manpages
  3. ^ "Native Win32 ports of some GNU utilities". unxutils.sourceforge.net.
  4. ^ IBM. "IBM System i Version 7.2 Programming Qshell" (PDF). IBM. Retrieved 2020-09-05.
  5. ^ IEEE Std 1003.1, 2004, documentation for test

Further reading

External links

This page was last edited on 1 January 2024, at 23:19
Basis of this page is in Wikipedia. Text is available under the CC BY-SA 3.0 Unported License. Non-text media are available under their specified licenses. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc. WIKI 2 is an independent company and has no affiliation with Wikimedia Foundation.