[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Help with --regex in locate



On Wed, 4 Dec 2019 at 01:36, <rhkramer@gmail.com> wrote:

> I always have trouble with all the rigamarole around quoting for the shell vs.
> quoting for the regex (or quoting or not quoting for anything else).
>
> I don't know what it will take to get it to sink into my head.  (Maybe in my
> next life ;-)

Hi, yes, it's complicated and confusing.

Here's a suggestion ...

Apologies if some of the following is stating
things that are obvious, I just try to give
complete instructions.

We can make a helper command that shows us
exactly what the shell does to any arguments
before it sends them to a command.

First we need a name for this helper command.
Let's use the name show_args.bash, but you
can use whatever name you like.

I suggest this name because it is unlikely to
already be in use on your system. Let's check:

$ type show_args.bash
bash: type: show_args.bash: not found

Ok, that name is not in use, so let's use it.
We are not going to put this command into
the PATH so name collision doesn't really
matter, but I'm just giving ideas and trying
to be thorough.

Open your favourite editor and create
a file that contains the next paragraph
(use cut and paste if possible, it must be
exactly as shown):

#!/bin/bash
printf '%s' "$# args:"
printf ' [%s]' "$@"
printf '\n'

Save this file with the name: show_args.bash

Now make that file executable:

$ chmod -v u+x show_args.bash
mode of 'show_args.bash' changed from 0640 (rw-r-----) to 0740 (rwxr-----)

Now we have created a command that just
displays the arguments that it receives from
the shell.

The command is in the current directory
and not in the PATH, so we need to specify
its location when invoking it: ./show_args.bash

Here's what it shows for a couple of commands
that you had trouble with.

$ ./show_args.bash locate --regex \/\.gitignore
3 args: [locate] [--regex] [/.gitignore]

The above shows that your attempt to put \.
into the regex did not work because the shell
removed that escape-quoting.

$ ./show_args.bash locate --regex '/\.gitignore'
3 args: [locate] [--regex] [/\.gitignore]

The above shows that adding single quotes
prevented that quote removal and the \.
remained in the regex argument that was
seen by locate.

A helper command like this is useful when
trying to apply the explanations at:
http://mywiki.wooledge.org/Quotes
to your own requirements.


Reply to: