4

I've got bash script for counting rows in the reports. I have one array where all reports names are stored and in the loop I'm counting rows. However for some files my script receives binary operator expected error. Do anyone have a solution?

for i in ${ARRAY[@]}; do
if [ ! -f "$BASE_DIR/$i"* ];
then
        echo "File not generated yet"
else
        ARRAY2=$(wc -l < "$BASE_DIR/$i"*.tab | awk '{print $1-2}')
        echo ${ARRAY2[$i]} $i
fi
3
  • You might be aware of the fact that * in this context will do globbing Commented Jul 1, 2016 at 11:47
  • globbing is used on purpose, cause there is also text before filename and extension Commented Jul 1, 2016 at 11:56
  • 1
    sjsam thanks for help, there was problem Commented Jul 1, 2016 at 12:09

2 Answers 2

2

Use double straight braces instead of ones as follows since you r using extended expressions.

if [[ ! -f "$BASE_DIR/$i"* ]];

Need to check with array contents. Special characters as ' ' (spaces) in file names must be escaped.

Sign up to request clarification or add additional context in comments.

Comments

2

-f takes just one argument, so the error occurs when the pattern matches more than one file.

It seems to work with [[, although I can't find any documentation as to why it does.

The bigger problem is you can also only use one file with the < operator; if the pattern matches multiple files, you'll get an ambiguous redirect error. To fix that, you'll need to use cat:

cat "$BASE_DIR/$i"*.tab | wc -l

However, it's not clear what you are expecting from the output; ARRAY2 will not actually be an array.

1 Comment

yeah, as @sjsam has mentioned the problem is with globbing, I have found out that I have some files with .bak in the end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.