Oxycash
Oxycash

Reputation: 197

wc -l giving integer expression expected error

I know the output isn\t being recognized as integer but I fail to understand why? The file has more than 5 lines.

$ if [ "$(wc -l test.txt)" -gt "$5" ]; then
            echo changes found;
           else
            echo  No changes found;
           fi

bash: [: 6 test.xml: integer expression expected
No changes found

Upvotes: 1

Views: 275

Answers (1)

Dominique
Dominique

Reputation: 17533

I just tried this:

wc -l test.txt

With following result:

5 test.txt

So the result contains a number, followed by the name of the file.

You can solve it like this:

wc -l test.txt | awk '{print $1}'

This only gives the number.

So, you might change your code into:

$ if [ "$(wc -l test.txt  | awk '{print $1}')" -gt "$5" ]; then

... and now correct:

$ if [ $(wc -l test.txt  | awk '{print $1}') -gt 5 ]; then

Upvotes: 3

Related Questions