Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Stupidly, I had been using a condition like this as part of a script:

if [ $(ls FOO* 2> /dev/null) ] # if files named "FOO*" were downloaded 
then
    echo "Files found" 
    # ... process and email results
else
    echo "Not found"
    # ... email warning that no files were found (against expectations)
fi

That works for zero and one files named FOO*, but fails if there are more than one. From logs I found several different error messages stemming from this:

[: FOO_20131107_082920: unary operator expected
[: FOO_20131108_070203: binary operator expected
[: too many arguments

My question is: what is the correct way to check, in a Bash if condition, whether one or more files whose name begins with FOO exist?

GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu)

share|improve this question

2 Answers 2

up vote 8 down vote accepted

This happens because your command substitution for ls outputs whitespace, and it ultimately undergoes word splitting before being passed to [. A less breakable way would be to put the files in an array, and then check that the array has at least one member.

shopt -s nullglob

files=( FOO* )
if (( ${#files[@]} )); then
    # there were files
fi

This works because (( by default returns true if the value does not equal 0, and ${#files[@]} gets the number of items in the array (which will be >0 if there are files matching the glob).

You could also do something like this, as long as nullglob is not set:

if ls FOO* >/dev/null 2>&1; then
    # there were files
fi

This just checks the exit code of ls, which will be 1 if you passed a filename that doesn't exist (the literal FOO*, if nothing is matched (unless, of course, you are evil and there is a file named FOO*, in which case it will return 0 :-) )).

Note that both of these also match directories. If you really only want to match regular files, you need to test that:

for file in FOO*; do
    if [[ -f $file ]]; then
        # file found, do some stuff and break
        break
    fi
fi
share|improve this answer
    
Thanks, this works. I've never seen shopt -s nullglob before but it makes the difference here. I just hope it doesn't trigger any problems in the remainder of the script (at least it does weird things to normal Bash shell). Can you turn that mode off later in the script? –  Jonik Nov 12 '13 at 10:07
1  
@Jonik Sure, just call shopt -u nullglob. It could cause problems if your script does some things that rely on POSIX null globbing behaviour. help shopt may also give you some useful information about it. –  Chris Down Nov 12 '13 at 10:10

There is another option, using case,

FILES=FOO*
FILES=$(echo $FILES)
case $FILES in FOO\*) echo "Not found" ;; esac

This works as case expects only one argument to test.

share|improve this answer
    
This doesn't work if there's a file called FOO*. –  Gilles Nov 12 '13 at 23:28

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.