Robert Elz <[email protected]> wrote:
 |This is the one that in one of the messages I just sent I said 
 |"not previously posted here" ...   When there is a leading/trailing '
 |it will be slower...
 |
 |Aside: to be really correct, without "local" it should really clean up
 |the vars (in a trap) in case it is interrupted by a signal, but I don't
 |think I will bother...  "local" is just so much better!
 |
 |kre
 |
 |quote() {
  ...

So this version really works, and i will keep it in my pocket of
jewels.  In turn, this reveals problems in many shells which are
in use, given the input string

  x 'a \ b' "foo'" "\\'b\\a\\r\\" Aä

(to get around missing support for $'').  Whereas my MUA with all
of its problems naturally gets it right

  vpospar set x 'a \ b' "foo'" "\\'b\\a\\r\\" Aä
  echo "$#: <${1}><${2}><${3}><$4><$5>"
  vput vpospar x quote; eval vpospar set ${x}
  echo $#: <${1}><${2}><${3}><$4><$5>
  echo $x
  xit

  5: <x><a \ b><foo'><\'b\a\r\><Aä>
  5: <x><a \ b><foo'><\'b\a\r\><Aä>
  x 'a \ b' $'foo\'' $'\\\'b\\a\\r\\' $'A\u00E4'

in the shells with

  [ "$1" = robert ] && quote=robert_quote
  [ "$1" = stephane ] && quote=stephane_quote
  [ -z "$quote" ] && { echo >&2 'robert or stephane'; exit 1; }
  set -- x 'a \ b' "foo'" "\\'b\\a\\r\\" Aä
  echo "$#: <${1}><${2}><${3}><$4><$5>"
  saved_parameters=$($quote "$@")
  eval "set -- $saved_parameters"
  echo "$#: <${1}><${2}><${3}><$4><$5>"

where robert_quote is indeed (thanks, Robert Elz!)

  robert_quote() {
          j=0
          for i
          do
                  [ $j -gt 0 ] && printf ' '
                  j=$((j + 1))
                  robert__quote "$i"
          done
  }

then of the tested only bash gets it right!

  $ for i in dash mksh bash; do \
    echo $i;$i t.sh robert; $i t.sh stephane; \
  done
  dash
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  mksh
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  \><Aä><a \ b><foo'><\'b
  bash
  5: <x><a \ b><foo'><\'b\a\r\><Aä>
  5: <x><a \ b><foo'><\'b\a\r\><Aä>
  5: <x><a \ b><foo'><\'b\a\r\><Aä>
  5: <x><a \ b><foo'><\'b\a\r\><Aä>

--steffen
|
|Ralph says i must not use signatures which spread the light!

Reply via email to