if test in bash - can't see error

Hi,
I have the following script under bash

        if [ test -f "$file_name" ]; then
             echo File $file_name Not Found
        else
             echo File $file_name Found
        fi

I get the message

[: -f: binary operator expected

even the file is found; there is only one file with that name in the directory

I can't figure out what's wrong.
Thanks in advance,

Hi

Either this

  if  test -f "$file_name" 

or this:

if [ -f "$file_name" ]
1 Like

So how the final script should be e.g. in the first case?

if [ -f "$file_name" ]; then
        echo File $file_name Found
else
        echo File $file_name Not Found
fi

Alternate one ..

$ [ -f "$file_name" ] && echo "Filename $file_name Found" || echo "Filename $file_name Not Found"
1 Like

If I use

if [ -f "$file_name" ]; then
        echo File $file_name Found
else
        echo File $file_name Not Found
fi

then it works in the opposite direction I hae the impression, i.e. the first if detects non existing file