10 # func_fatal_error message
11 # outputs to stderr a fatal error message, and terminates the program.
14 echo "test-echo.sh: *** $1" 1>&2
15 echo "test-echo.sh: *** Stop." 1>&2
19 # Ensure an 'echo' command that does not interpret backslashes.
21 # echo '\n' | wc -l prints 1 when OK, 2 when KO
22 # echo '\t' | grep t > /dev/null has return code 0 when OK, 1 when KO
23 # This problem is a weird heritage from SVR4. BSD got it right.
24 # Nowadays the problem occurs in 4 situations:
25 # - in bash, when the shell option xpg_echo is set,
26 # - in zsh, when sh-emulation is not set,
27 # - in ksh (e.g. AIX /bin/sh and Solaris /usr/xpg4/bin/sh are ksh instances,
28 # and HP-UX /bin/sh and IRIX /bin/sh behave similarly),
29 # - in Solaris /bin/sh and OSF/1 /bin/sh.
30 # We try the following workarounds:
31 # - for all: respawn using $CONFIG_SHELL if that is set and works.
32 # - for bash: unset the shell option xpg_echo.
33 # - for zsh: turn sh-emulation on.
34 # - for ksh: alias echo to a function that uses cat of a here document.
35 # - for Solaris /bin/sh: respawn using /bin/ksh and rely on the ksh workaround.
36 # - otherwise: respawn using /bin/sh and rely on the workarounds.
37 # When respawning, we pass --no-reexec as first argument, so as to avoid
38 # turning this script into a fork bomb in unlucky situations.
40 if echo '\t' | grep t > /dev/null; then
41 have_echo=yes # Lucky!
43 # Try the workarounds.
44 # Respawn using $CONFIG_SHELL if that is set and works.
45 if test -z "$have_echo" \
46 && test "X$1" != "X--no-reexec" \
47 && test -n "$CONFIG_SHELL" \
48 && test -f "$CONFIG_SHELL" \
49 && $CONFIG_SHELL -c 'echo '\t' | grep t > /dev/null'; then
50 exec $CONFIG_SHELL "$0" --no-reexec "$@"
53 # For bash: unset the shell option xpg_echo.
54 if test -z "$have_echo" \
55 && test -n "$BASH_VERSION" \
56 && (shopt -o xpg_echo; echo '\t' | grep t > /dev/null) 2>/dev/null; then
60 # For zsh: turn sh-emulation on.
61 if test -z "$have_echo" \
62 && test -n "$ZSH_VERSION" \
63 && (emulate sh) >/dev/null 2>&1; then
66 # For ksh: alias echo to a function that uses cat of a here document.
67 # The ksh manual page says:
68 # "Aliasing is performed when scripts are read, not while they are executed.
69 # Therefore, for an alias to take effect, the alias definition command has
70 # to be executed before the command which references the alias is read."
71 # Because of this, we have to play strange tricks with have_echo, to ensure
72 # that the top-level statement containing the test start after the 'alias'
74 if test -z "$have_echo"; then
81 alias echo=bsd_echo 2>/dev/null
83 if test -z "$have_echo" \
84 && echo '\t' | grep t > /dev/null; then
87 if test -z "$have_echo"; then
88 unalias echo 2>/dev/null
90 # For Solaris /bin/sh and OSF/1 /bin/sh: respawn using /bin/ksh.
91 if test -z "$have_echo" \
92 && test "X$1" != "X--no-reexec" \
93 && test -f /bin/ksh; then
94 exec /bin/ksh "$0" --no-reexec "$@"
97 # Otherwise: respawn using /bin/sh.
98 if test -z "$have_echo" \
99 && test "X$1" != "X--no-reexec" \
100 && test -f /bin/sh; then
101 exec /bin/sh "$0" --no-reexec "$@"
104 if test -z "$have_echo"; then
105 func_fatal_error "Shell does not support 'echo' correctly. Please install GNU bash and set the environment variable CONFIG_SHELL to point to it."
107 if echo '\t' | grep t > /dev/null; then
110 func_fatal_error "Shell does not support 'echo' correctly. Workaround does not work. Please report this as a bug to bug-gnulib@gnu.org."
112 if test "X$1" = "X--no-reexec"; then
116 # This command determines the exit code.
117 echo '\t' | grep t > /dev/null