Home > Blockchain >  wc -l giving integer expression expected error
wc -l giving integer expression expected error

Time:01-18

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

CodePudding user response:

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
  •  Tags:  
  • Related