Subject: bug#36764: grep -q throwing up No such file or directory error

From: Assaf Gordon <[email-masked]>
Date: Mon, 22 Jul 2019 16:16:43 -0600

tag 36764 notabug
close 36764
stop

Hello,

(answering out-of-order)

On Mon, Jul 22, 2019 at 06:12:10PM +0100, Lewis Farnworth wrote:

> To reiterate:
> 
> if grep -q "thing_i'm_looking_for" $variable_i'm_looking_in; then
> 
> doesn't work, on CentOS 7 or Debian 9.

This is incorrect usage, and has never worked with gnu grep.
The correct usage is:

   grep -q "thing_i'm_looking_for" "FILE to look in"

Or, to check the content of a variable, use:

   echo "$variable_i'm_looking_in" | grep -q "$thing_i'm_looking_for"

or the more robust:

   printf "%s" "$variable_i'm_looking_in" | grep -q "$thing_i'm_looking_for"

> I previously had a working script, that used -q in this context here:
> 
> if grep -q "RewriteCond %{HTTP_REFERER} !.*example.com*" $line; then
> 
> -----------------------------------------------------------------------------------------------------
> That syntax was perfectly functioning as a conditional inside of a while
> loop, to detect a colleague's error inside of a bunch of .htaccess rules.

As for the above, I would guess that the "while" loop read a FILENAME
into the variable $line (e.g. a path of an .htaccess file),
and then grep would search for the pattern in that file.

If you have access to the machine where the above works,
add "echo LINE=$line" to print the content of the variable,
and you'll see it contains a file name.

> This time, I'm trying to basically ascertain which servers on a given
> domain are using Gmail. I've ran some dig, grep, sed & awk to ascertain a
> list of mail servers the domain is using. One of the outputs which I'm
> looking for looks something like this:
> 
> $   echo $mailServ:
> alt3.aspmx.l.google.com.
> alt4.aspmx.l.google.com.
> alt1.aspmx.l.google.com.
> alt2.aspmx.l.google.com.
> aspmx.l.google.com.
> 
> *The script I'm running:*
> 
> if grep -q "google.com" $mailServ; then
> printf "%s uses google for mail \n" $mailServ
> fi
> 
> I've run into a few weird error messages when trying to use the -q
> option... And I'm 100% certain that this is not a syntax issue (but there's
> a part of me wishing it is).
> 
> *The error I get is: *
> 
> grep: aspmx.l.google.com.: No such file or directory
> grep: alt1.aspmx.l.google.com.: No such file or directory
> grep: alt4.aspmx.l.google.com.: No such file or directory
> grep: alt2.aspmx.l.google.com.: No such file or directory
> grep: alt3.aspmx.l.google.com.: No such file or directory

This is expected, as explained above.

> When in actual fact, all I need is the  printf command output. I tried to
> run the exact same script on Debian 9, to ensure that was not the issue...
> Apparently that doesn't work either.

Use something like:

  if echo "$mailServ" | grep -q "google.com"; then
     printf "%s uses google for mail \n" $mailServ
  fi

Note that this operates on the entire content of "$mailServ" (all lines
together), not line-by-line.

> I tried to use the first script I wrote again, for the sake of a sanity
> check, and that script is now broken too.

If an unmodified script used to work on CentOS before an upgrade,
and stops working after the upgrade (again, unmodified),
and you've isolated the issue to grep, please provide more details
(e.g. the entire script).

As such, I'm closing this as "not a bug", but discussion can continue
by replying to this thread.

regards,
 - assaf

P.S.
Note that in your regex patterns you use "." as a literal dot,
but it is in fact a regex special character meaning "any character".
So:

     grep "google.com" $FILE

would also match a file containing "googleXcom".

To match a literal dot, use "\.".