Check if WC Command output is greather than in BASH
Asked Answered
S

1

9

I need to check if wc command output is greather than a variable. Here's my code:

if test wc -w $i -gt $num
then
   echo "too great"
fi

If the current file $i contains more words than the $num variable i print "too great". I already tried all but can't get the script working.

Steelworks answered 23/3, 2020 at 10:33 Comment(0)
C
9

You need to take the output of the wc command and use it as an argument to test:

if test "$( wc -w < "$i" )" -gt "$num"

See "Command Substitution" in man bash.

If you don't use redirection <, wc also outputs the file name, which would break the comparison.

Commodore answered 23/3, 2020 at 10:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.