You are not logged in.

#1 2021-05-16 19:41:36

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,248
Website

Bash parameter search and replace with multiple changes possible?

I read through this guide: https://wiki.bash-hackers.org/syntax/pe … nd_replace

I'm wondering if I one can substitute multiple occurrences within the same expression.  For example, let's say I want to both substitute x86-64 with haswell and simultaneously replace -D_FORTIFY_SOURCE=2, with a null.

CFLAGS="-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2,-D_GLIBCXX_ASSERTIONS -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection"

I can do each substitution but I do not know how to string them together:

echo ${CFLAGS/x86-64/haswell}
-march=haswell -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_FORTIFY_SOURCE=2,-D_GLIBCXX_ASSERTIONS -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection
echo ${CFLAGS/-D_FORTIFY_SOURCE=2,/}
-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -fexceptions -Wp,-D_GLIBCXX_ASSERTIONS -Wformat -Werror=format-security -fstack-clash-protection -fcf-protection

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2021-05-16 20:14:02

seth
Member
Registered: 2012-09-03
Posts: 21,101

Re: Bash parameter search and replace with multiple changes possible?

I "cache" the substitution in the/a variable then then run a second substitution on that, ie.

FOO=bar; FOO=${FOO/b/f}; echo ${FOO/ar/oo}

And I mostly post to subscribe to the topic, cause curious myself ;-)
(But I honestly don't think that it's possible to somehow link substitutions in one expansion)

Offline

#3 2021-05-16 20:20:55

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,248
Website

Re: Bash parameter search and replace with multiple changes possible?

@seth - Yep, this is the approach I took, but wanted to see if someone with more knowledge or better search-Fu has an answer wink


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2021-05-16 20:36:15

seth
Member
Registered: 2012-09-03
Posts: 21,101

Re: Bash parameter search and replace with multiple changes possible?

You're probably talking about some wizard here … tongue

Offline

#5 2021-05-16 20:47:27

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,280

Re: Bash parameter search and replace with multiple changes possible?

Why modify, just append.  Appended flags will override earlier statements of the same option on the command line.  If you want -march=whatever:

echo $CFLAGS -march=whatever

Whatever other -march flags were already in CFLAGS before this are now irrelevant.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2021-05-16 22:06:06

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,248
Website

Re: Bash parameter search and replace with multiple changes possible?

Thanks, Trilby.  In one case I need to replace a switch with a null so still two steps.

CFLAGS+=" -march=haswell"
CFLAGS=${CFLAGS/-D_FORTIFY_SOURCE=2,/}

CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#7 2021-05-16 23:21:31

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 25,280

Re: Bash parameter search and replace with multiple changes possible?

You don't need to replace it with "null" do you, as there is a default (isn't 1 the default?).  So just overwrite it with the default.  But even if you really really do need it removed, you still don't need two steps:

gcc-or-whatever ${CFLAGS/-D_FORTIFY_SOURCE=2,//} -march=haswell

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB