2

I've found the following in a regular expression:

s/.*\`\(.*\)'/\1/g

I'm wondering what the grave accent means. I have only a very basic understanding of regular expressions, so I tried to look it up, but a basic search didn't lead me to anything useful.

The context: I'm trying out the accepted answer to this question: An error building Qt libraries for the raspberry pi

It suggests running the following script to adjust simlinks:

find . -maxdepth 1 -type l | while read i;
do qualifies=$(file $i | sed -e "s/.*\`\(.*\)'/\1/g" | grep ^/lib)
if [ -n "$qualifies" ]; then
newPath=$(file $i | sed -e "s/.*\`\(.*\)'/\1/g" | sed -e "s,\`,,g" | sed -e "s,',,g" | sed -e "s,^/lib,$2/lib,g");
echo $i
echo $newPath;
sudo rm $i;
sudo ln -s $newPath $i;
fi
done

However, it doesn't do anything, so I'm trying to understand how it is supposed to work.

The find finds a lot of files, an example:

./libcap-ng.so.0
./libnettle.so.4
./libSDL-1.2.so.0
./libwebpmux.so.1
./libnss_nisplus.so
./libavahi-gobject.so.0
./libgdk-3.so.0
./librsvg-2.so.2
./libatk-bridge-2.0.so.0
./libgeoclue.so.0
./libdv.so.4
./libcdda_interface.so.0
./libheimbase.so.1
./libfontconfig.so.1
./libXinerama.so.1
./libopenal.so.1
./libicule.so.52 
./libaudio.so.2
./libmplex2-2.1.so.0

and so on for dozens of other files, but none of them seem to "qualify" for the regex.

Community
  • 1
  • 1
vsz
  • 4,811
  • 7
  • 41
  • 78

1 Answers1

4

In Bash, double-quoted strings "..." allow various expansions, including command substitution with $(...) or `...`. For example, this:

cat "fo`echo ob`ar"

is equivalent to cat foobar, because `echo ob` means "run the command echo ob and plop its output here".

So if you want to include an actual ` in a double-quoted string, you have to quote it with a backslash. And that's what your script is doing. sed -e "s,\`,,g", for example, just means sed -e 's,`,,g', i.e. "remove all occurrences of `". (Note that the ` doesn't mean anything special to sed itself; it just matches ` in the input. The same command could have been written as tr -d '`'.)

ruakh
  • 175,680
  • 26
  • 273
  • 307
  • This means that it just removes grave accents and anything between brackets... and after that, it accepts anything with "/lib" in it. I wonder why none of the files "qualify". Could you please provide an example of a string which would qualify? I think I'm still missing something. – vsz Oct 21 '15 at 06:22
  • Re: "it just removes grave accents and anything between brackets": That's not correct. `\(` and `\)` don't denote brackets; rather, they're used for grouping and capturing. And `s` denotes *replacement*, not *removal*. The reason that `s,\`,,g` is removal-only is that it doesn't have a replacement string; but `"s/.*\`\(.*\)'/\1/g"` *does* have one. `"s/.*\`\(.*\)'/\1/g"` means "find anything of the form `foo\`bar'`, and replace it with just the `bar` part". – ruakh Oct 21 '15 at 14:55
  • I still wonder *why* they though it was necessary, but at least it's clear *how* it works. Thanks! – vsz Oct 21 '15 at 15:03