Converting string to int doesn't work properly in terminal.

I want to find how much time specific string occur in a big string.

if ((echo "iOS/i" | grep -o iOS/ | wc -l | bc)>2) then echo "ThreeOrMore"; else echo"TwoOrLess"; fi



But the result actually should be TwoOrLess

but inner part works well

echo "iOS/i" | grep -o iOS | wc -l | bc



You can't use > to compare numbers in bash (> is used to redirect output to a file), you need to run the comparison as part of a test/[ command. Also the call to bc doesn't seem to do anything, echo "iOS/i" | grep -o iOS | wc -l already outputs 1.

The updated snippet looks like this (split on several lines for easier readability):

if [ $(echo "iOS/i" | grep -o iOS/ | wc -l) -gt 2 ]; then
    echo "ThreeOrMore"
    echo "TwoOrLess"
  • 1
    So | wc -l is needed. I tried to omit wc -l, but this expression didn't returned 3 : echo "iOS/iOS/iOS" | grep -o -c iOS – Voloda2 Apr 4 '15 at 15:27
  • Ah, looks like I just learned something. Thanks, I'll edit the answer accordingly. – nohillside Apr 4 '15 at 15:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .