File check script fails for multiple files

I want to check if any file with testing*.txt exists but my script fails if more than 1 file exists. It works fine for a single file

if [ -s /tmp/file_checker/testing*.txt ]
then
echo "TEST21"
fi

--------------

bash: [: too many arguments

How do I fix this?

Thanks

for f in /tmp/file_checker/testing*.txt; do
  [ -s "$f" ] && echo TEST21 && break
done

Note that -s returns true if the file exists and its size is greater than zero.

1 Like

Another approach:

if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then
echo "TEST21"
fi
1 Like

Bipinajith: Thanks for your response. Is there a way to supress this message if the file foes not exist?

ls: /tmp/file_checker/testing*.txt: No such file or directory

Basically I want to display just the message that the file does not exist:

if ls /tmp/file_checker/testing*.txt > /dev/null; then
echo "TEST21"
else echo "file does not exist"
fi

Redirect stderr to /dev/null like I suggested above:

if ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null; then
1 Like

Works like a charm.

Thanks

---------- Post updated at 12:27 PM ---------- Previous update was at 12:06 PM ----------

Sorry to bother you again. I need to check 2 conditions to be true and it seems ls does not support -a "AND" operation verification

something like this:

VAR1="FOO"
if [ $ORACLE_SID = $VAR1 -a ls /tmp/file_checker/testing*.txt ] > /dev/null 2> /dev/null; then
echo "TEST21"
else echo "file does not exist"
fi

Thanks

It was outside [ ] before, but you put it in [ ] , where it expects an expression not a command.

if [ "$ORACLE_SID" = "$VAR1" ] && ls /tmp/file_checker/testing*.txt >/dev/null 2>/dev/null
then
...
else
...
fi
1 Like
VAR1="FOO"
if [ "$ORACLE_SID" = "$VAR1" ] && ls /tmp/file_checker/testing*.txt > /dev/null 2> /dev/null
then
    echo "TEST21"
else 
    echo "file does not exist"
fi
1 Like

Works!

Thanks

---------- Post updated at 12:35 PM ---------- Previous update was at 12:34 PM ----------

Thanks Bipinajith works!