Too many arguments error

Here's my code.

     21 if [ -f addr*.tmp ]; then
     22
     23 ls addr*.tmp | while IFS= read -r entry; do           # Starts Reading .cfg files one by one ...
     24 echo $entry

I get the below error at line number 21

line 21: [: too many arguments

Can you tell me how can i alter my code to avoid the error ?

Maybe like this:

set addr*.tmp
if [ -f "$1" ]; then 
   echo "got at least one addr*.tmp file"
fi

Or better...

for tmpfile addr*.tmp ; do
   if [ -f "$tmpfile" ]; then 
   echo "got at least one addr*.tmp file"
   fi
   break
done

if there is no file matching the pattern, the pattern stays as written and when tested, no file exists with that pattern.

1 Like

-f only accepts one argument, when there's two or more matching files that causes a syntax error.

The code above can work but beware that set will change your commandline parameters (unless done inside a function).

1 Like

A BIG thank you and resolved