Skip to contentSkip to conversation

Improve sed portability

PATCH In review Chris Ridd 5 participants 9 messages Jun 15, 2016

lore
Share & tools
lore

Chris Ridd

author
#0 lore raw

PATCH 1/1 Improve sed portability

On Solaris /usr/bin/sed apparently fails to process input that doesn't end in a \n. Consequently constructs like

  re=$(printf '%s' foo | sed -e 's/bar/BAR/g' $)

cause re to be set to the empty string. Such a construct is used in git-submodule.sh.

Changing the printf to add a \n seems the safest change. The POSIX-compliant seds shipped with Solaris do not have this problem.

---
 git-submodule.sh |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
git-submodule.sh +1 -1
index 1007372..e515bcc 100755
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
 	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
-	re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+	re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')
 	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )
        test -z "$name" &&
Signature · 1 line
1.5.3.6
#1 lore raw
Quoted 6 lines · Chris Ridd

On Solaris /usr/bin/sed apparently fails to process input that doesn't end in a \n. Consequently constructs like

  re=$(printf '%s' foo | sed -e 's/bar/BAR/g' $)

cause re to be set to the empty string.

So does /usr/bin/sed of AIX 4.3!

git-submodule.sh resolve_relative_url () · line 76

Quoted 6 lines
git-submodule.sh +1 -1
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
 	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
-	re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+	re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')

You change sq into dq. Is this not dangerous? Shouldn't backslash-en be hidden from the shell so that printf can interpret it?

git-submodule.sh resolve_relative_url () · line 78

Quoted 2 lines
git-submodule.sh +0 -0
 	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )

I trust you have tested this. But I wonder whether this leaves a stray newline in $re that gets in the way inside the sed expression...

git-submodule.sh resolve_relative_url () · line 79

Quoted 1 line
git-submodule.sh +0 -0
        test -z "$name" &&

-- Hannes

author
#2 lore raw
Quoted 9 lines · Johannes Sixt
Quoted 6 lines · Chris Ridd

On Solaris /usr/bin/sed apparently fails to process input that doesn't end in a \n. Consequently constructs like

  re=$(printf '%s' foo | sed -e 's/bar/BAR/g' $)

cause re to be set to the empty string.

So does /usr/bin/sed of AIX 4.3!

I ought to have mentioned this occurs on Solaris 8, 10, build 90 of OpenSolaris, and on HP-UX 11iv1. I stared at that regex for quite a while before realising the problem was with the input :-)

Quoted 9 lines

git-submodule.sh resolve_relative_url () · line 76

Quoted 6 lines
git-submodule.sh +1 -1
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
 	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
-	re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+	re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')

You change sq into dq. Is this not dangerous? Shouldn't backslash-en be hidden from the shell so that printf can interpret it?

It is necessary to use double quotes. This:

printf '%s\n' foobar

prints a literal \, a literal n, and no newline:

foobar\n

Not desirable :-(

Of course, using a plain old:

    echo "$1"

should work well too. Why is printf being used here and not echo, anyway?

Quoted 5 lines

git-submodule.sh resolve_relative_url () · line 78

Quoted 2 lines
git-submodule.sh +0 -0
 	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )

I trust you have tested this. But I wonder whether this leaves a stray newline in $re that gets in the way inside the sed expression...

Yes, I've tested this as we use submodules heavily. I think the $( .. ) notation will remove the trailing \n printed by sed, but to be sure I inserted a 'set -x' at the top of the module_name() function and double-checked that the re variable didn't get any stray \n character(s). Bash versions 2 and 3 were used.

So without the change, on Solaris I get:

No submodule mapping found in .gitmodules for path 'foobar'

for the first submodule that we use, and the repository clone fails.

With the change, all our repositories clone OK.

Cheers,

Chris

#3 lore raw
Quoted 9 lines · Chris Ridd

It is necessary to use double quotes. This:

printf '%s\n' foobar

prints a literal \, a literal n, and no newline:

foobar\n

Not desirable :-(

On what platform?

Quoted 5 lines

Of course, using a plain old:

    echo "$1"

should work well too. Why is printf being used here and not echo, anyway?

Because the original didn't have a newline, and "echo -n" isn't portable?

-Peff

#4 lore raw
In reply to Chris Ridd
Quoted 15 lines · Chris Ridd
Quoted 11 lines · Johannes Sixt

git-submodule.sh resolve_relative_url () · line 76

Quoted 7 lines · Chris Ridd
git-submodule.sh +0 -0
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
     # Do we have "submodule.<something>.path = $1" defined in

.gitmodules file?

git-submodule.sh +1 -1
-    re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+    re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')

You change sq into dq. Is this not dangerous? Shouldn't backslash-en be hidden from the shell so that printf can interpret it?

It is necessary to use double quotes. This:

Quoted 7 lines

printf '%s\n' foobar

prints a literal \, a literal n, and no newline:

foobar\n

Not desirable :-(

On both Linux and AIX 4.3 I see:

$  printf 'x\ny'; echo z
x
yz

The printf turns the \n into LF.

I mentioned this in the first place because I don't know what various shells do with \n when they see "%s\n". But one way or the other, the \n will be turned into LF, either by the shell or by printf. So it's not a big deal.

Quoted 5 lines

Of course, using a plain old:

    echo "$1"

should work well too. Why is printf being used here and not echo, anyway?

Because the "$1" could contain character sequences that some 'echo' implementations mangle.

-- Hannes

author
#5 lore raw
Quoted 22 lines · Johannes Sixt
Quoted 13 lines · Chris Ridd
Quoted 10 lines · Johannes Sixt

git-submodule.sh resolve_relative_url () · line 76

Quoted 7 lines · Chris Ridd
git-submodule.sh +0 -0
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
     # Do we have "submodule.<something>.path = $1" defined in

.gitmodules file?

git-submodule.sh +1 -1
-    re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+    re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')

You change sq into dq. Is this not dangerous? Shouldn't backslash-en be hidden from the shell so that printf can interpret it?

It is necessary to use double quotes. This:

Quoted 7 lines

printf '%s\n' foobar

prints a literal \, a literal n, and no newline:

foobar\n

Not desirable :-(

Quoted 7 lines

On both Linux and AIX 4.3 I see:

$  printf 'x\ny'; echo z
x
yz

The printf turns the \n into LF.

Yes, and I don't know *what* I did yesterday, but Solaris 8, 10, (every OS I mentioned before) behave the same as your test.

I did actually have my eyes tested later on yesterday :-)

Quoted 4 lines

I mentioned this in the first place because I don't know what various shells do with \n when they see "%s\n". But one way or the other, the \n will be turned into LF, either by the shell or by printf. So it's not a big deal.

I agree.

Quoted 8 lines
Quoted 5 lines

Of course, using a plain old:

    echo "$1"

should work well too. Why is printf being used here and not echo, anyway?

Because the "$1" could contain character sequences that some 'echo' implementations mangle.

Indeed. If $1 started with -n that might cause problems on some platforms.

Should I revise my commit to use single quotes again?

Cheers,

Chris

#6 lore raw
Quoted 4 lines · Chris Ridd
Quoted 2 lines

Because the "$1" could contain character sequences that some 'echo' implementations mangle.

Indeed. If $1 started with -n that might cause problems on some platforms.

It's much worse than that. Any backslash sequence can be interpolated. 4b7cc26 (git-am: use printf instead of echo on user-supplied strings).

-Peff

#7 lore raw
In reply to Chris Ridd
Quoted 6 lines · Chris Ridd

Of course, using a plain old:

     echo "$1"

should work well too. Why is printf being used here and not echo, anyway?

Uh, because 'echo -n' is not portable enough, and for some reason it was though that there shouldn't be final newline?

Signature · 3 lines
Jakub Narebski
Poland
ShadeHawk on #git
#8 lore raw
In reply to Johannes Sixt
Quoted 10 lines · Johannes Sixt
Quoted 6 lines · Chris Ridd

On Solaris /usr/bin/sed apparently fails to process input that doesn't end in a \n. Consequently constructs like

  re=$(printf '%s' foo | sed -e 's/bar/BAR/g' $)

cause re to be set to the empty string.

So does /usr/bin/sed of AIX 4.3!

Quoted 9 lines

git-submodule.sh resolve_relative_url () · line 76

Quoted 6 lines
git-submodule.sh +1 -1
@@ -73,7 +73,7 @@ resolve_relative_url ()
 module_name()
 {
 	# Do we have "submodule.<something>.path = $1" defined in .gitmodules file?
-	re=$(printf '%s' "$1" | sed -e 's/[].[^$\\*]/\\&/g')
+	re=$(printf "%s\n" "$1" | sed -e 's/[].[^$\\*]/\\&/g')

You change sq into dq. Is this not dangerous? Shouldn't backslash-en be hidden from the shell so that printf can interpret it?

"\n" inside dq is _not_ interpreted by the shell (printf interprets it), but I tend to agree that using sq is worry-free and better.

Quoted 5 lines

git-submodule.sh resolve_relative_url () · line 78

Quoted 2 lines
git-submodule.sh +0 -0
 	name=$( git config -f .gitmodules --get-regexp '^submodule\..*\.path$' |
 		sed -n -e 's|^submodule\.\(.*\)\.path '"$re"'$|\1|p' )

I trust you have tested this. But I wonder whether this leaves a stray newline in $re that gets in the way inside the sed expression...

I suspect the very original was written (or copied from something that wrote) like this:

	re=$(echo -n "$1" | sed -e '...')

and mechanically replaced to

	re=$(printf '%s' "$1" | sed -e '...')

because "echo" is not quite portable.

But the original misunderstands the command substitution. The trailing LF is removed by it, so as long as "$1" is a single line, $re will get a line without the trailing LF _anyway_.