[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
grep: return code with -v and empty input
From: |
Edward Avis |
Subject: |
grep: return code with -v and empty input |
Date: |
Mon, 19 Mar 2001 15:06:28 +0000 (GMT) |
According to the documentation, the -v flag is supposed to invert the
exit status:
>Normally, exit status is 0 if matches were found, and 1 if no matches
>were found (the `-v' option inverts the sense of the exit status).
However it doesn't seem to work like that for empty input. Here's some
commands I ran, the shell I used was bash2:
% if echo a | grep -q a; then echo found; else echo not found; fi
found
% if echo b | grep -q a; then echo found; else echo not found; fi
not found
% if : | grep -q a; then echo found; else echo not found; fi
not found
So far so good - the pattern was 'not found' in empty input, as you'd
expect. But what about with the -v flag and the if-test inverted?
% if echo a | grep -qv a; then echo not found; else echo found; fi
found
% if echo b | grep -qv a; then echo not found; else echo found; fi
not found
% if : | grep -qv a; then echo not found; else echo found; fi
found
I suspect this may be a documentation bug; perhaps it should say
something like:
'If the -v flag is given, then a "matching line" is one which does _not_
match the pattern. The exit status is still 0 if there were no
"matching lines" and 1 if there were some.'
--
Ed Avis
address@hidden
- grep: return code with -v and empty input,
Edward Avis <=