User xenoid us right, pathname expansion does not occur in [[
expressions.
From the bash man page:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.
Expressions are composed of the primaries described below under CONDITIONAL EXPRESSIONS
.
Word splitting and pathname expansion are not performed on the words between the [[
and ]]
; tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal are performed. Conditional operators such as -f
must be unquoted to be recognized as primaries.
(emphasis mine)
To use the [[
test, you need to do the expansion before the actual test.
This could be done with the help of an array to store the generated filenames.
Below are two examples:
The first tests for one matching file and the the second for at least one matching file and breaks out of the loop if a match was found.
files=( /tmp/test.*.lock )
# test if exactly one file matches
if [[ "${#files[@]}" -eq 1 ]] && [[ -e "${files[0]}" ]]; then
echo "file ${files[0]} exists!"
fi
# test if at least one file matches
for f in "${files[@]}"; do
if [[ -e "$f" ]]; then
echo "at least one file exists!"
break
fi
done
Note that you don't need quotes around variables in [[
expressions, but it doesn't hurt to do so and you could replace [[
with [
.
If you know that your lock file is a regular file, use -f
instead.