bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: aliasing "sed" to "sed --posix"


From: Bruno Haible
Subject: Re: aliasing "sed" to "sed --posix"
Date: Sun, 16 Sep 2007 02:41:43 +0200
User-agent: KMail/1.5.4

Bruce Korb wrote on 2007-08-06:
> Well, I knew ahead of writing the script that unless the shopt
> expand_aliases were turned on that they would not work.  I had
> stumbled into this before.  I experimented with aliasing "exit"
> because it would have a dramatic effect on the execution flow. :)
> Sure enough, if I aliased "exit" to the echo, the echo fired
> and I had demonstrated my point:  if you want to ensure that
> "sed" is run with "--posix", then either ensure that aliases
> are expanded within the script, or else use a function.  This
> is true whether you are aliasing sed or, apparently, exit.

Thanks for this advice. Defining a function 'func_sed' requires massive
changes to gnulib-tool; we already went through this once, and it proved
too fragile to maintain. Defining a function 'sed' require the determination
of the full name of the 'sed' executable, which is problematic due to missing
"test -x" support and due to $EXEEXT. So, here aliases are better. Let's
turn them in bash; this is the common case.

Bruno


2007-09-15  Bruno Haible  <address@hidden>

        * gnulib-tool (sed): Try a little harder to make bash understand the
        alias.
        Reported by Bruce Korb <address@hidden>.

*** gnulib-tool 9 Sep 2007 12:17:36 -0000       1.257
--- gnulib-tool 16 Sep 2007 00:40:48 -0000
***************
*** 73,78 ****
--- 73,89 ----
  # gnulib-tool generates, since we don't want "sed --posix" to leak
  # into makefiles.
  if (alias) > /dev/null 2>&1 && echo | sed --posix -e d >/dev/null 2>&1; then
+   # Define sed as an alias.
+   # It is not always possible to use aliases. Aliases are guaranteed to work
+   # if the executing shell is bash and either it is invoked as /bin/sh or
+   # is a version >= 2.0, supporting shopt. This is the common case.
+   # Two other approaches (use of a variable $sed or of a function func_sed
+   # instead of an alias) require massive, fragile code changes.
+   # An other approach (use of function sed) requires `which sed` - but 'which'
+   # is hard to emulate, due to missing "test -x" on some platforms.
+   if test -n "$BASH_VERSION"; then
+     shopt -s expand_aliases >/dev/null 2>&1
+   fi
    alias sed='sed --posix'
  fi
  





reply via email to

[Prev in Thread] Current Thread [Next in Thread]