-- Linux commands, scripts, tools and systems administration --
test command in BASH and the man page to find it
TEST command example (from man page 1p):
digging into a recent (2014) release "test" was discovered, the man page (1p) provided examples.
to determine the exit status of "test" $?
$? reads the exit status of the last command executed.
After a function returns, $? gives the exit status of the last command executed in the function.
IF SUCCESSFUL it is 0
This is the BASH "return value." Testing an expression
it can be determined if the test was true or false by displaying $?.
SUCCESSFUL:
-----------
echo hello
echo $? # Exit status 0 returned because command executed successfully.
UNSUCCESSFUL:
-----------
lskdf # Unrecognized command.
echo $? # Non-zero exit status returned -- command failed to execute
--> lskdf
If 'lskdf' is not a typo you can use command-not-found to lookup the package that contains it, like this:
cnf lskdf
------------------------------------------------
--> echo $?
127
From man page (link below)
2. Perform a mkdir if a directory does not exist:
test... if tempdir doesn't exist, make it...
test ! -d tempdir && mkdir tempdir
from an prior script used to build systems (circa 1999):
testing using an if statement... if the directory LOGS doesn't exist, then make it...
if [ ! -d ~/bin/LOGS ]
then
/bin/mkdir ~/bin/LOGS
/bin/echo "LOGS directory in ~/bin made"
fi
From man page (link below)
4. Perform a command if the argument is one of three strings (two variations):
(NOTE: no "test" command, just the function in this example: the two pipe symbols represent an OR function)
if [ "$1" = "pear" ] || [ "$1" = "grape" ] || [ "$1" = "apple" ]
then
command
fi
-----------------------------------
case "$1" in
pear|grape|apple) command ;;
esac
-----------------------------------
In trying to understand the "test" command, the man pages for section 1 did not completely describe how to use
this command or where it originated. The 1p page explained it is used to evaluate and expression, but the output of the command was
not clearly stated. The section 1p provided examples that explained its use and history.
In loops based on a test, it was previously done by brackets, [] but the test command was introduced to simplify.
To get to the correct section of the man page:
man --usage
Usage: man [-dDfkKlwWciIau7tZ?V] [-C FILE] [-R ENCODING] [-L LOCALE]
[-m SYSTEM] [-M PATH] [-S LIST] [-s LIST] [-e EXTENSION]
[-P PAGER] [-r STRING] [-E ENCODING] [-p STRING] [-T[DEVICE]]
[-H[BROWSER]] [-X[RESOLUTION]] [--config-file=FILE] [--debug]
[--default] [--warnings[=WARNINGS]] [--whatis] [--apropos]
[--global-apropos] [--local-file] [--where] [--path] [--location]
[--where-cat] [--location-cat] [--catman] [--recode=ENCODING]
[--locale=LOCALE] [--systems=SYSTEM] [--manpath=PATH]
[--sections=LIST] [--extension=EXTENSION] [--ignore-case]
[--match-case] [--regex] [--wildcard] [--names-only] [--all]
[--update] [--no-subpages] [--pager=PAGER] [--prompt=STRING]
[--ascii] [--encoding=ENCODING] [--no-hyphenation] [--nh]
[--no-justification] [--nj] [--preprocessor=STRING] [--troff]
[--troff-device[=DEVICE]] [--html[=BROWSER]]
[--gxditview[=RESOLUTION]] [--ditroff] [--help] [--usage]
[--version] [SECTION] PAGE...
man --sections=1p test
|