Steven Penny wrote in <CAAXzdLUeByjci1nxvo34gJR=UU26n6d2CPr11p6M7Rb_eNXC\
[email protected]>:
|On Mon, May 27, 2019 at 12:12 PM Steffen Nurpmeso wrote:
|> Actually only one. Because that works on all shells i have
|> tested, even on SunOS 5.9 /bin/sh. If you do not care about
|> portability why not use mksh or bash directly (in the shebang)?
|
|I am not interested in a brittle function that leaks variables into \
|the global
|scope:
|
| $ quote_rndtrip aa
| 'aa'
|
| $ echo "[$i]"
| [aa]
|
| $ echo "[$j]"
| [ ]
|
|I am interested in having Dash source code modified such that it supports \
|arrays
|natively. If you have a patch for that I would be interested, thank you.
Oh yeah, that is surely my fault then, not kre's. That thus needs
to be changed, let's enclose the outer driver(s) with subshell(s)
here -- local variables are unfortunately not truly portable
either.
How about the following, including s///g changes for better names?
#!/bin/sh -
#@ Round trip quote strings in POSIX shell. E.g.,
#@ set -- x 'a \ b' "foo'" "\\'b\\a\\r\\" Aä
#@ printf "%s: <%s><%s><%s><%s><%s>\n" "$#" "${1}" "${2}" "${3}" "$4" "$5"
#@ saved_parameters=`quote_rndtrip "$@"`
#@ eval "set -- $saved_parameters"
#@ printf "%s: <%s><%s><%s><%s><%s>\n" "$#" "${1}" "${2}" "${3}" "$4" "$5"
#
# Public Domain
# Though slower use a subshell version instead of properly restoring $IFS
# and flags, as elder shells may not be able to properly restore flags via
# "set +o" as later standardized in POSIX, and it seems overkill to handle
# all possible forms of output "set +o" may or may not actually generate.
quote__rndtrip() (
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
r="${1}"
shift
for a
do
r="${r}'\\''${a}"
done
printf "${s}'%s'${e}" "${r}"
exit 0
)
quote_rndtrip() (
j=
for i
do
[ -n "$j" ] && printf ' '
j=' '
quote__rndtrip "$i"
done
)
quote_string() (
j=
for i
do
[ -n "$j" ] && printf '\\ '
j=' '
quote__rndtrip "$i"
done
)
--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)