From 47b0bff399ab0cca893b38b876d526fb3f088833 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 23 Jun 2007 09:52:08 +0000 Subject: [PATCH] Add better support for bash versions < 2.04. --- ChangeLog | 5 +++++ gnulib-tool | 41 +++++++++++++++++++++++++++++++++++------ tests/test-echo.sh | 39 ++++++++++++++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 34ebacd1e5..b869adcdd3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2007-06-23 Bruno Haible + + * gnulib-tool (echo): Add workarounds also for bash versions < 2.04. + * tests/test-echo.sh: Likewise. + 2007-06-23 Ralf Wildenhues * gnulib-tool (IFS): Initialize early, so we don't set it to diff --git a/gnulib-tool b/gnulib-tool index ef8e9ac89c..a84b0f1812 100755 --- a/gnulib-tool +++ b/gnulib-tool @@ -22,7 +22,7 @@ progname=$0 package=gnulib -cvsdatestamp='$Date: 2007-06-23 07:40:58 $' +cvsdatestamp='$Date: 2007-06-23 09:52:09 $' last_checkin_date=`echo "$cvsdatestamp" | sed -e 's,^\$[D]ate: ,,'` version=`echo "$last_checkin_date" | sed -e 's/ .*$//' -e 's,/,-,g'` nl=' @@ -415,16 +415,21 @@ func_ln_if_changed () # Test cases: # echo '\n' | wc -l prints 1 when OK, 2 when KO # echo '\t' | grep t > /dev/null has return code 0 when OK, 1 when KO -# This problem is a weird heritage from SVR4. BSD got it right. +# This problem is a weird heritage from SVR4. BSD got it right (except that +# BSD echo interprets '-n' as an option, which is also not desirable). # Nowadays the problem occurs in 4 situations: -# - in bash, when the shell option xpg_echo is set, +# - in bash, when the shell option xpg_echo is set (bash >= 2.04) +# or when it was built with --enable-usg-echo-default (bash >= 2.0) +# or when it was built with DEFAULT_ECHO_TO_USG (bash < 2.0), # - in zsh, when sh-emulation is not set, # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances, # and HP-UX /bin/sh and IRIX /bin/sh behave similarly), # - in Solaris /bin/sh and OSF/1 /bin/sh. # We try the following workarounds: # - for all: respawn using $CONFIG_SHELL if that is set and works. -# - for bash: unset the shell option xpg_echo. +# - for bash >= 2.04: unset the shell option xpg_echo. +# - for bash >= 2.0: define echo to a function that uses the printf built-in. +# - for bash < 2.0: define echo to a function that uses cat of a here document. # - for zsh: turn sh-emulation on. # - for ksh: alias echo to a function that uses cat of a here document. # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround. @@ -445,13 +450,37 @@ if test -z "$have_echo" \ exec $CONFIG_SHELL "$0" --no-reexec "$@" exit 127 fi -# For bash: unset the shell option xpg_echo. +# For bash >= 2.04: unset the shell option xpg_echo. if test -z "$have_echo" \ && test -n "$BASH_VERSION" \ && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then shopt -o xpg_echo have_echo=yes fi +# For bash >= 2.0: define echo to a function that uses the printf built-in. +# For bash < 2.0: define echo to a function that uses cat of a here document. +# (There is no win in using 'printf' over 'cat' if it is not a shell built-in.) +if test -z "$have_echo" \ + && test -n "$BASH_VERSION"; then \ + if type printf 2>/dev/null | grep / > /dev/null; then + # 'printf' is not a shell built-in. +echo () +{ +cat < /dev/null; then + have_echo=yes + fi +fi # For zsh: turn sh-emulation on. if test -z "$have_echo" \ && test -n "$ZSH_VERSION" \ @@ -464,7 +493,7 @@ fi # Therefore, for an alias to take effect, the alias definition command has # to be executed before the command which references the alias is read." # Because of this, we have to play strange tricks with have_echo, to ensure -# that the top-level statement containing the test start after the 'alias' +# that the top-level statement containing the test starts after the 'alias' # command. if test -z "$have_echo"; then bsd_echo () diff --git a/tests/test-echo.sh b/tests/test-echo.sh index e0c47a7af3..45eabd7336 100755 --- a/tests/test-echo.sh +++ b/tests/test-echo.sh @@ -20,16 +20,21 @@ func_fatal_error () # Test cases: # echo '\n' | wc -l prints 1 when OK, 2 when KO # echo '\t' | grep t > /dev/null has return code 0 when OK, 1 when KO -# This problem is a weird heritage from SVR4. BSD got it right. +# This problem is a weird heritage from SVR4. BSD got it right (except that +# BSD echo interprets '-n' as an option, which is also not desirable). # Nowadays the problem occurs in 4 situations: -# - in bash, when the shell option xpg_echo is set, +# - in bash, when the shell option xpg_echo is set (bash >= 2.04) +# or when it was built with --enable-usg-echo-default (bash >= 2.0) +# or when it was built with DEFAULT_ECHO_TO_USG (bash < 2.0), # - in zsh, when sh-emulation is not set, # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances, # and HP-UX /bin/sh and IRIX /bin/sh behave similarly), # - in Solaris /bin/sh and OSF/1 /bin/sh. # We try the following workarounds: # - for all: respawn using $CONFIG_SHELL if that is set and works. -# - for bash: unset the shell option xpg_echo. +# - for bash >= 2.04: unset the shell option xpg_echo. +# - for bash >= 2.0: define echo to a function that uses the printf built-in. +# - for bash < 2.0: define echo to a function that uses cat of a here document. # - for zsh: turn sh-emulation on. # - for ksh: alias echo to a function that uses cat of a here document. # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround. @@ -50,13 +55,37 @@ if test -z "$have_echo" \ exec $CONFIG_SHELL "$0" --no-reexec "$@" exit 127 fi -# For bash: unset the shell option xpg_echo. +# For bash >= 2.04: unset the shell option xpg_echo. if test -z "$have_echo" \ && test -n "$BASH_VERSION" \ && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then shopt -o xpg_echo have_echo=yes fi +# For bash >= 2.0: define echo to a function that uses the printf built-in. +# For bash < 2.0: define echo to a function that uses cat of a here document. +# (There is no win in using 'printf' over 'cat' if it is not a shell built-in.) +if test -z "$have_echo" \ + && test -n "$BASH_VERSION"; then \ + if type printf 2>/dev/null | grep / > /dev/null; then + # 'printf' is not a shell built-in. +echo () +{ +cat < /dev/null; then + have_echo=yes + fi +fi # For zsh: turn sh-emulation on. if test -z "$have_echo" \ && test -n "$ZSH_VERSION" \ @@ -69,7 +98,7 @@ fi # Therefore, for an alias to take effect, the alias definition command has # to be executed before the command which references the alias is read." # Because of this, we have to play strange tricks with have_echo, to ensure -# that the top-level statement containing the test start after the 'alias' +# that the top-level statement containing the test starts after the 'alias' # command. if test -z "$have_echo"; then bsd_echo () -- 2.30.2