Robert Elz wrote in <14296.1494903...@andromeda.noi.kre.to>:
 |    Date:        Mon, 15 May 2017 18:36:58 +0200
 |    From:        Steffen Nurpmeso <stef...@sdaoden.eu>
 |    Message-ID:  <20170515163658.b7ljs%stef...@sdaoden.eu>
 |
 || Is it at all possible to store the parameter stack in a variable
 || "x" in order to restore it exactly "as-is", in the shell?
 |
 |Yes, it is, you just need to think in a slightly different way when
 ...
 |What you need first is a function which quotes a string, used like
 | result=$(quote "$string")
 |in a way that you can later do
 | eval "string=${result}"
 |and get back the original string - whatever it contains.
 ...
 |quote() {
 ...
 |}
 ...
 --End of <14296.1494903...@andromeda.noi.kre.to>

Robert Elz wrote in <8267.1494911...@andromeda.noi.kre.to>:
  ...
 |And of course, you could also write it as a sub-shell function - as \
 |this func
 |has no intended effects on the calling shell, so doing it that way avoids
 |problems of namespace pollution, or any requirement that IFS or the options
 |be restored.  To do that, instead of
 |
 |quote() {
 | # code...
 |}
 |
 |write
 |
 |quote() (
 | # code, but not bothering with any save/restore and using any var names
 |)
 ...

For compatibilities sake i want to add that the subshell version
is necessary on Sun OS 5.9, as /usr/xpg4/bin/sh does not support
set +o "in a format that is suitable for reinput":

  #?0|unstable9s:sdaoden$ /usr/xpg4/bin/sh -c 'i=`set +o`; echo $i'
  Current option settings allexport off bgnice off

So the only portable in-shell code seems to be

  kre__quote() (
     case "$1" in
     *\'*) ;;
     *) printf "'%s'" "$1"; return 0;;
     esac
     __A__="$1" __S__= __E__=
     while case "$__A__" in
        \'*)  __A__=${__A__#?}; __S__="${__S__}\\\\'";;
        *\')  __A__=${__A__%?}; __E__="${__E__}\\\\'";;
        '')   printf "${__S__}${__E__}"; exit 0;;
        *) false;;
        esac
     do
        continue
     done
     IFS=\'
     set -f
     set -- $__A__
     _result_="${1}"
          shift
     for __A__
     do
        _result_="${_result_}'\\''${__A__}"
     done
     printf "${__S__}'%s'${__E__}" "${_result_}"
     exit 0
  )
  kre_quote() {
     j=
     for i
     do
        [ -n "$j" ] && printf ' '
        j=' '
        kre__quote "$i"
     done
  }

with the precaution that printf is not a built-in sometimes.

 |kre
 --End of <8267.1494911...@andromeda.noi.kre.to>

With many thanks for your efforts, once again.

--steffen
|
|Der Kragenbaer,                The moon bear,
|der holt sich munter           he cheerfully and one by one
|einen nach dem anderen runter  wa.ks himself off
|(By Robert Gernhardt)

Reply via email to