Bash - issue with grep conditional statement
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
Why does this code work correctly, while the other version of the same condition doesn't?
if grep -q string file; then
echo found
else
echo not found
fi
This doesn't work:
if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi
bash grep test
add a comment |Â
up vote
0
down vote
favorite
Why does this code work correctly, while the other version of the same condition doesn't?
if grep -q string file; then
echo found
else
echo not found
fi
This doesn't work:
if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi
bash grep test
1
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Do you know what-q
does, or are you just blindly using it because you've seen others use it?
â chepner
Oct 21 '17 at 1:27
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Why does this code work correctly, while the other version of the same condition doesn't?
if grep -q string file; then
echo found
else
echo not found
fi
This doesn't work:
if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi
bash grep test
Why does this code work correctly, while the other version of the same condition doesn't?
if grep -q string file; then
echo found
else
echo not found
fi
This doesn't work:
if [ ! `grep -q string file` ]; then
echo not found
else
echo found
fi
bash grep test
edited Oct 20 '17 at 9:27
Jeff Schaller
32.1k849109
32.1k849109
asked Oct 20 '17 at 8:56
asd
33
33
1
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Do you know what-q
does, or are you just blindly using it because you've seen others use it?
â chepner
Oct 21 '17 at 1:27
add a comment |Â
1
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Do you know what-q
does, or are you just blindly using it because you've seen others use it?
â chepner
Oct 21 '17 at 1:27
1
1
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Do you know what
-q
does, or are you just blindly using it because you've seen others use it?â chepner
Oct 21 '17 at 1:27
Do you know what
-q
does, or are you just blindly using it because you've seen others use it?â chepner
Oct 21 '17 at 1:27
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
`grep -q string file`
, in backticks (or inside $(...)
, which is preferable), will be replaced by the output of grep
. This will be an empty string since -q
is used.
To negate a test, just insert !
before it:
if ! grep -q pattern file; then
echo not found
else
echo found
fi
If you truly want to search for a string (rather than a regular expression), then you should use -F
with grep
as well.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
`grep -q string file`
, in backticks (or inside $(...)
, which is preferable), will be replaced by the output of grep
. This will be an empty string since -q
is used.
To negate a test, just insert !
before it:
if ! grep -q pattern file; then
echo not found
else
echo found
fi
If you truly want to search for a string (rather than a regular expression), then you should use -F
with grep
as well.
add a comment |Â
up vote
5
down vote
accepted
`grep -q string file`
, in backticks (or inside $(...)
, which is preferable), will be replaced by the output of grep
. This will be an empty string since -q
is used.
To negate a test, just insert !
before it:
if ! grep -q pattern file; then
echo not found
else
echo found
fi
If you truly want to search for a string (rather than a regular expression), then you should use -F
with grep
as well.
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
`grep -q string file`
, in backticks (or inside $(...)
, which is preferable), will be replaced by the output of grep
. This will be an empty string since -q
is used.
To negate a test, just insert !
before it:
if ! grep -q pattern file; then
echo not found
else
echo found
fi
If you truly want to search for a string (rather than a regular expression), then you should use -F
with grep
as well.
`grep -q string file`
, in backticks (or inside $(...)
, which is preferable), will be replaced by the output of grep
. This will be an empty string since -q
is used.
To negate a test, just insert !
before it:
if ! grep -q pattern file; then
echo not found
else
echo found
fi
If you truly want to search for a string (rather than a regular expression), then you should use -F
with grep
as well.
edited Oct 20 '17 at 11:07
answered Oct 20 '17 at 9:19
Kusalananda
105k14209326
105k14209326
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f399303%2fbash-issue-with-grep-conditional-statement%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Why have you changed the syntax on the two examples?, just a ! to your original example
â Raman Sailopal
Oct 20 '17 at 9:04
Do you know what
-q
does, or are you just blindly using it because you've seen others use it?â chepner
Oct 21 '17 at 1:27