Bash test: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
(Created page with "=External= * http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html =Internal= * Linux * ln =Negation= <pre> [ ! -f ${...")
 
 
(30 intermediate revisions by the same user not shown)
Line 2: Line 2:


* http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
* http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html
* http://tldp.org/LDP/abs/html/testconstructs.html


=Internal=
=Internal=
Line 7: Line 8:
* [[Linux#Commands|Linux]]
* [[Linux#Commands|Linux]]
* [[ln]]
* [[ln]]
=Overview=
'test' is a bash built-in command which tests the expression following it, according to the syntax described below. 'test' and [[Bash_Concepts#Tests_.5B....5D_.5B.5B....5D.5D|[...]]] are synonymous and can be used interchangeably.


=Negation=
=Negation=


<pre>
[ ! -f ${file} ] && echo "we're not a file" || echo "we're a file"
[ ! -f ${file} ] && echo "we're not a file" || echo "we're a file"
 
</pre>
=-e=
 
True if file exists (regardless of type).
 
=-s=
 
True if file exists and has a size greater than zero.
 
For a zero-length test, use:
[[Wc#-c|wc -c]]
 
This also works:
<syntaxhighlight lang='bash'>
if [[ -f ${file} ]] && [[ ! -s ${file} ]]; then
  echo "empty"
fi
</syntaxhighlight>


=-f=
=-f=
True if file exists and is a regular file.
=-r=
True if file exists and is readable.


=-w=
=-w=
Line 24: Line 51:
Tests whether the file exists and it is a symbolic link.
Tests whether the file exists and it is a symbolic link.


<pre>
[ -h $0 ] && echo "we're a link" || echo "we're not a link"
[ -h $0 ] && echo "we're a link" || echo "we're not a link"
 
</pre>
=-v=
 
True if the variable specified after <code>-v</code> has been allocated, even if it has an empty value (blank string):
 
For the following cases -v some_var evaluates to true:
 
some_var=
 
some_var=""
 
some_var="something"
 
If the variable is not mentioned at all -v some_var evaluates to false.
 
In case of indexed or associative arrays, declarations with "declare -a" or "declare -A" are not detected by -v. Only if an array was actually allocated -v returns true. For more details, see [[Bash_Arrays#Indexed_Array_Declaration_Test|Indexed Array Declaration Test]], [[Bash_Arrays#Indexed_Array_Allocation_Test|Indexed Array Allocation Test]], [[Bash_Arrays#Associative_Array_Declaration_Test|Associative Array Declaration Test]] and [[Bash_Arrays#Associative_Array_Allocation_Test|Associative Array Allocation Test]].
 
=-x=
 
True if file exists and is executable.


=Integer Comparison=
=Integer Comparison=
Line 33: Line 78:
[arg1 -eq|-ne|-lt|-le|-gt|-ge arg2]
[arg1 -eq|-ne|-lt|-le|-gt|-ge arg2]
</pre>
</pre>
=String Comparison=
< and > can be used for string comparison, in ASCII alphabetical order, but the [[Bash_Expressions#.5B.5B....5D.5D|extended test command &#91;[ ... ]]]]:
if [[ "${a}" > "${b}" ]]; them
    ...
fi
=File Time Comparison=
==-nt==
<syntaxhighlight lang='bash'>
if [[ ${file1} -nt ${file2} ]]; then
  ...
fi
</syntaxhighlight>
==-ot==


=-z, -n=
=-z, -n=
Line 60: Line 122:
=Combining test and Command Conditionals=
=Combining test and Command Conditionals=


<font color=red>Not sure if this belongs here, refactor with https://home.feodorov.com:9443/wiki/Wiki.jsp?page=bashConditionalConstructs</font>
<font color=red>Not sure if this belongs here, refactor with http://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs</font>


<pre>
<syntaxhighlight lang='bash'>
verbose=false
verbose=false


Line 68: Line 130:
     echo "setting verbose to ON"
     echo "setting verbose to ON"
fi
fi
</pre>
</syntaxhighlight>


<blockquote style="background-color: Gold; border: solid thin Goldenrod;">
{{Warn|Must have a space between ! and the command that evaluates to true or false.}}
:Must have a space between ! and the command that evaluates to true or false.<br>
</blockquote>

Latest revision as of 18:07, 1 April 2024

External

Internal

Overview

'test' is a bash built-in command which tests the expression following it, according to the syntax described below. 'test' and [...] are synonymous and can be used interchangeably.

Negation

[ ! -f ${file} ] && echo "we're not a file" || echo "we're a file"

-e

True if file exists (regardless of type).

-s

True if file exists and has a size greater than zero.

For a zero-length test, use:

wc -c

This also works:

if [[ -f ${file} ]] && [[ ! -s ${file} ]]; then
  echo "empty"
fi

-f

True if file exists and is a regular file.

-r

True if file exists and is readable.

-w

True if file exists and is writable.

-h

Tests whether the file exists and it is a symbolic link.

[ -h $0 ] && echo "we're a link" || echo "we're not a link"

-v

True if the variable specified after -v has been allocated, even if it has an empty value (blank string):

For the following cases -v some_var evaluates to true:

some_var=
some_var=""
some_var="something"

If the variable is not mentioned at all -v some_var evaluates to false.

In case of indexed or associative arrays, declarations with "declare -a" or "declare -A" are not detected by -v. Only if an array was actually allocated -v returns true. For more details, see Indexed Array Declaration Test, Indexed Array Allocation Test, Associative Array Declaration Test and Associative Array Allocation Test.

-x

True if file exists and is executable.

Integer Comparison

[arg1 -eq|-ne|-lt|-le|-gt|-ge arg2]

String Comparison

< and > can be used for string comparison, in ASCII alphabetical order, but the extended test command [[ ... ]]:

if [[ "${a}" > "${b}" ]]; them
    ...
fi

File Time Comparison

-nt

if [[ ${file1} -nt ${file2} ]]; then
  ...
fi

-ot

-z, -n

-z: true if the length of the string is zero.

-n: true if the length of string is non-zero.

Can be used to check for undeclared variables

[ -z ${var} ] && echo" 'var' not declared"
Quotation marks around the variables are needed, otherwise the expression will fail syntactically when the variable is declared and it contains a space:
var="a b"

...

line 177: [: 900: binary operator expected

Combining test and Command Conditionals

Not sure if this belongs here, refactor with http://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#Conditional-Constructs

verbose=false

if ! ${verbose} && [ "$1" = "--verbose" -o "$1" = "-v" ]; then
    echo "setting verbose to ON"
fi

Must have a space between ! and the command that evaluates to true or false.