Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

How do I evaluate or calculate the return value of a command line? For exemple, I count the number of lines with a grep and I want to know if that value is above X. If so, I want to print the number to a file.

Or I want to substract the value of a grep count to another grep count...

How can I manipulate return values that way ?

share|improve this question
add comment

1 Answer 1

up vote 10 down vote accepted

I think you are mixing two things the return value typically indicates if a command was successful (return value 0) or not (anything else). You can get the return value of a command from the variable $?

grep -c returns the count to stdout, to capture the count you can use something like

variable=$(grep -c pattern filename)

Afterwords you can calculate/access the variable how ever you want. See How can I do command line integer & float calculations, in bash, or any language available? how to calculate stuff with the output.

share|improve this answer
    
Yeah that was what I meant. And your answer is perfect ! –  Philippe Carriere Jul 4 '12 at 19:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.