[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [PATCH 3/5] configure: Don't use bash-specific string-replacement sy
From: |
Eric Blake |
Subject: |
Re: [PATCH 3/5] configure: Don't use bash-specific string-replacement syntax |
Date: |
Wed, 20 Jul 2022 11:29:54 -0500 |
User-agent: |
NeoMutt/20220429 |
On Wed, Jul 20, 2022 at 04:26:29PM +0100, Peter Maydell wrote:
> The variable string-replacement syntax ${var/old/new} is a bashism
> (though it is also supported by some other shells), and for instance
> does not work with the NetBSD /bin/sh, which complains:
> ../src/configure: 687: Syntax error: Bad substitution
>
> Replace it with a more portable sed-based approach, similar to
> what we already do in quote_sh().
>
> for e in $1; do
> - e=${e/'\'/'\\'}
> - e=${e/\"/'\"'}
> - printf '"""%s""",' "$e"
> + printf '"""'
> + # backslash escape any '\' and '"' characters
> + printf "%s" "$e" | sed -e 's/\([\"]\)/\\\1/g'
You've fixed the bashism, but at the expense of a non-POSIX use of
sed. POSIX says the input to sed must be a text file (ending in a
newline; but $e does not), and as a result it always outputs a newline
(but you don't want a newline before the closing """). GNU sed
happens to do what you want for input not ending in a newline, but I
don't remember off-hand whether BSD sed does, and I know that Solaris
sed does not.
If this passes on BSD, then I'm okay with it; but if we want to avoid
non-POSIX altogether, this should work (using the shell's $() to strip
the trailing newline we added to keep sed happy):
# backslash escape any '\' and '"' characters
printf '"""%s""",' "$(printf "%s\n" "$e" | sed -e '/s/\([\"]\)/\\\1/g')"
Your call.
--
Eric Blake, Principal Software Engineer
Red Hat, Inc. +1-919-301-3266
Virtualization: qemu.org | libvirt.org