substring.sh revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
########################################################################
# #
# This software is part of the ast package #
# Copyright (c) 1982-2012 AT&T Intellectual Property #
# and is licensed under the #
# Eclipse Public License, Version 1.0 #
# by AT&T Intellectual Property #
# #
# A copy of the License is available at #
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
# #
# Information and Software Systems Research #
# AT&T Research #
# Florham Park NJ #
# #
# David Korn <dgk@research.att.com> #
# #
########################################################################
function err_exit
{
print -u2 -n "\t"
let Errors+=1
}
alias err_exit='err_exit $LINENO'
then err_exit "string1:0"
fi
then err_exit "string1: -1"
fi
then err_exit "string1:0"
fi
then err_exit "string1:1"
fi
then err_exit "string1:1:4"
fi
then err_exit "string1: -5:4"
fi
then err_exit "string1:1:j"
fi
then err_exit "string1:(j?1:0):j"
fi
then err_exit "string1%*zzz*"
fi
then err_exit "string1%%*zzz*"
fi
if [[ ${string1#*zzz*} != "$string1" ]]
then err_exit "string1#*zzz*"
fi
if [[ ${string1##*zzz*} != "$string1" ]]
then err_exit "string1##*zzz*"
fi
then err_exit "string1%+(abc)"
fi
then err_exit "string1%%+(abc)"
fi
then err_exit "string1%/*"
fi
if [[ "${string1%/*}" != "$base" ]]
then err_exit '"string1%/*"'
fi
then err_exit 'string1%"/*"'
fi
if [[ ${string1%%/*} != "" ]]
then err_exit "string1%%/*"
fi
if [[ ${string1#*/bar} != /abcabcabc ]]
then err_exit "string1#*bar"
fi
if [[ ${string1##*/bar} != /abcabcabc ]]
then err_exit "string1#*bar"
fi
then err_exit "string1#@(*/bar|*/foo)"
fi
if [[ ${string1##@(*/bar|*/foo)} != /abcabcabc ]]
then err_exit "string1##@(*/bar|*/foo)"
fi
if [[ ${string1##*/@(bar|foo)} != /abcabcabc ]]
then err_exit "string1##*/@(bar|foo)"
fi
if [[ ${foo#a[b*} != abc ]]
then err_exit "abc#a[b*} != abc"
fi
then err_exit '${foo//[0-9]/bar} not expanding correctly'
fi
foo='(abc)'
if [[ ${foo#'('} != 'abc)' ]]
then err_exit "(abc)#( != abc)"
fi
then err_exit "(abc)%) != (abc"
fi
then err_exit '${foo/[0-9]?/""} not expanding correctly'
fi
then err_exit '${foo//[0-9]/""} not expanding correctly'
fi
if [[ ${foo/#a/b} != b123b456c ]]
then err_exit '${foo/#a/b} not expanding correctly'
fi
if [[ ${foo/#?/b} != b123b456c ]]
then err_exit '${foo/#?/b} not expanding correctly'
fi
then err_exit '${foo/%c/b} not expanding correctly'
fi
then err_exit '${foo/%?/b} not expanding correctly'
fi
fi
if [[ ${string##$pattern} != "" ]]
fi
if [ "${string##$pattern}" != '' ]
fi
fi
fi
fi
done <<- \EOF
xx=a/b/c/d/e
if [[ $yy != b/c/d/e ]]
then err_exit '${xx#*/} != a/b/c/d/e when xx=a/b/c/d/e'
fi
if [[ ${xx//\//\\} != 'a\b\c\d\e' ]]
then err_exit '${xx//\//\\} not working'
fi
x=[123]def
then err_exit 'closing brace escape not working'
fi
then err_exit '${xx//%28/abc\)} not working'
fi
xx='a:b'
str='(){}[]*?|&^%$#@l'
for ((i=0 ; i < ${#str}; i++))
do [[ $(eval print -r -- \"\${xx//:/\\${str:i:1}}\") == "a${str:i:1}b" ]] || err_exit "substitution of \\${str:i:1}} failed"
[[ $(eval print -rn -- \"\${xx//:/\'${str:i:1}\'}\") == "a${str:i:1}b" ]] || err_exit "substitution of '${str:i:1}' failed"
[[ $(eval print -r -- \"\${xx//:/\"${str:i:1}\"}\") == "a${str:i:1}b" ]] || err_exit "substitution of \"${str:i:1}\" failed"
done
unset foo
then err_exit 'single quoting / in replacements failed'
fi
then err_exit 'double quoting / in replacements failed'
fi
if [[ ${foo//\//_} != one_two_three ]]
then err_exit 'escaping / in replacements failed'
fi
function myexport
{
nameref var=$1
if (( $# > 1 ))
then export $1=$2
fi
if (( $# > 2 ))
return
fi
typeset val
print ${val#"$1="}
}
then err_exit 'export inside function not working'
fi
if [[ ${val#dgk=} != base ]]
then err_exit 'export not restored after function call'
fi
then err_exit 'export inside function not working with recursive function'
fi
if [[ ${val#dgk=} != base ]]
then err_exit 'export not restored after recursive function call'
fi
then err_exit 'name=value not added to export list with function call'
fi
if [[ ${val#dgk=} != base ]]
then err_exit 'export not restored name=value function call'
fi
unset zzz
then err_exit 'export inside function not working for zzz'
fi
then err_exit 'zzz exported after function call'
fi
if [[ ${@/*\/@(*)/\1} != 'bar yes done' ]]
then err_exit '\1 not working with $@'
fi
then err_exit '\1 not working with ${var[@]}'
fi
var='abc_d2ef.462abc %%'
then err_exit '${var/+(\w)/Q} not workding'
fi
then err_exit '${var//+(\w)/Q} not workding'
fi
then err_exit '${var//+(\S)/Q} not workding'
fi
[[ $var == 'typeset -a .sh.match=((vin vin) )' ]] || err_exit '.sh.match not correct when replacement pattern contains a substring match'
foo='foo+bar+'
unset x
x=abcedfg
: ${x%@(d)f@(g)}
: ${x%%+(d)f@(g)}
unset a b
a='\[abc @(*) def\]'
b='[abc 123 def]'
unset foo
[[ ${foo/'('/'(x11-'} == '(x11-win32.i386) ' ]] || err_exit "\${var/pattern} not working with ' in pattern"
$SHELL -c $'v=\'$(hello)\'; [[ ${v//\'$(\'/-I\'$(\'} == -I"$v" ]]' 2> /dev/null || err_exit "\${var/pattern} not working with \$( as pattern"
unset X
i=20030704
[[ ${i#{6}(?)} == 04 ]] || err_exit '${i#{6}(?)} not working'
[[ ${i#{6,6}(?)} == 04 ]] || err_exit '${i#{6,6}(?)} not working'
i=" ."
unset x
x=foo
integer i
for((i=0; i < ${#string}; i++))
do pattern+='@(?)'
done
[[ $(string=$string $SHELL -c ": \${string/$pattern/}; print \${.sh.match[26]}") == Z ]] || err_exit -u2 'sh.match[26] not Z'
D=$';' E=$'\\\\' Q=$'"' S=$'\'' M='nested pattern substitution failed'
x='-(-)-'
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
x='-(-)-)-'
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
x='-(-()-)-'
[[ ${x/*%(())*/\1} == '()' ]] || err_exit $M
x='-(-\)-)-'
[[ ${x/*%(())*/\1} == '(-\)' ]] || err_exit $M
x='-(-\\)-)-'
[[ ${x/*%(())*/\1} == '(-\\)' ]] || err_exit $M
x='-(-(-)-'
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
x='-(-(-)-)-'
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
x='-(-[-]-)-'
[[ ${x/*%(()[])*/\1} == '(-[-]-)' ]] || err_exit $M
x='-[-(-)-]-'
[[ ${x/*%(()[])*/\1} == '(-)' ]] || err_exit $M
x='-(-[-)-]-'
[[ ${x/*%(()[])*/\1} == '-(-[-)-]-' ]] || err_exit $M
x='-(-[-]-)-'
[[ ${x/*%([]())*/\1} == '[-]' ]] || err_exit $M
x='-[-(-)-]-'
[[ ${x/*%([]())*/\1} == '[-(-)-]' ]] || err_exit $M
x='-(-[-)-]-'
[[ ${x/*%([]())*/\1} == '-(-[-)-]-' ]] || err_exit $M
x='-((-))-'
[[ ${x/*%(())*/\1} == '(-)' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(-g)*%(())*/\1} == '((-))-' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(-g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(+g)*%(())*/\1} == '(-)' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(+g:*)*%(())*/\1} == '(-)' ]] || err_exit $M
x='-((-))-'
[[ ${x/*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(-g)*(?)*%(())*(?)*/:\1:\2:\3:} == '::((-))::-' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(-g:*(?))*%(())*(?)*/:\1:\2:\3:} == '::(-):)-:' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(+g)*(?)*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
x='-((-))-'
[[ ${x/~(+g:*(?))*%(())*(?)*/:\1:\2:\3:} == ':-(:(-):)-:' ]] || err_exit $M
x='call(a+b,x/(c/d),(0));'
[[ ${x/+([[:alnum:]])*([[:space:]])@(*%(()))*/:\1:\2:\3:} == ':call::(a+b,x/(c/d),(0)):' ]] || err_exit $M
x='-(-;-)-'
[[ ${x/*%(()D${D})*/\1} == '-(-;-)-' ]] || err_exit $M
x='-(-);-'
[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
x='-(-)\;-'
[[ ${x/*%(()D${D})*/\1} == '(-)' ]] || err_exit $M
x='-(-\;-)-'
[[ ${x/*%(()D${D}E${E})*/\1} == '(-\;-)' ]] || err_exit $M
x='-(-)\;-'
[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
x='-(-(-)\;-)-'
[[ ${x/*%(()D${D}E${E})*/\1} == '(-)' ]] || err_exit $M
x='-(-")"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
x=$'-(-\\\'")\\\'-)-'
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"\'-)-'
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\')\\\'\'-)-'
[[ ${x/*%(()Q${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
x=$'-(-\'")\'-)-'
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\'")\'-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"\'-)-'
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\')\\\'\'-)-'
[[ ${x/*%(()L${S}Q${Q})*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
x='-(-")"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-")"-)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-\")"-)' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()Q${Q})*/\1} == '(-\")\"-)' ]] || err_exit $M
x='-(-\)-)-'
[[ ${x/*%(()E${E})*/\1} == '(-\)-)' ]] || err_exit $M
x='-(-\\)-)-'
[[ ${x/*%(()E${E})*/\1} == '(-\\)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()E${E}Q${Q})*/\1} == '(-\")' ]] || err_exit $M
x=$'-(-\'")"-)-'
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()E${E}Q${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\\"\')\\\'\\"-)-'
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
x=$'-(-\\"\')\\\'\\"\'-)-'
[[ ${x/*%(()E${E}L${S}Q${Q})*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
x='-(-;-)-'
[[ ${x/*%(()D\;)*/\1} == '-(-;-)-' ]] || err_exit $M
x='-(-);-'
[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
x='-(-)\;-'
[[ ${x/*%(()D\;)*/\1} == '(-)' ]] || err_exit $M
x='-(-\;-)-'
[[ ${x/*%(()D\;E\\)*/\1} == '(-\;-)' ]] || err_exit $M
x='-(-);-'
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
x='-(-)\;-'
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
x='-(-(-)\;-)-'
[[ ${x/*%(()D\;E\\)*/\1} == '(-)' ]] || err_exit $M
x='-(-")"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
x=$'-(-\\\'")\\\'-)-'
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")\\\'-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"\'-)-'
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\')\\\'\'-)-'
[[ ${x/*%(()Q\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
x=$'-(-\'")\'-)-'
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\'")\'-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\\\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"\'-)-'
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\\'")"\'-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\')\\\'\'-)-'
[[ ${x/*%(()L\'Q\")*/\1} == $'-(-\')\\\'\'-)-' ]] || err_exit $M
x='-(-")"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-")"-)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-\")"-)' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()Q\")*/\1} == '(-\")\"-)' ]] || err_exit $M
x='-(-\)-)-'
[[ ${x/*%(()E\\)*/\1} == '(-\)-)' ]] || err_exit $M
x='-(-\\)-)-'
[[ ${x/*%(()E\\)*/\1} == '(-\\)' ]] || err_exit $M
x='-(-\")"-)-'
[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
x='-(-\")\"-)-'
[[ ${x/*%(()E\\Q\")*/\1} == '(-\")' ]] || err_exit $M
x=$'-(-\'")"-)-'
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'-(-\'")"-)-' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()E\\Q\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\\\'")"-)-'
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\\'")"-)' ]] || err_exit $M
x=$'-(-\\"\')\'\\"-)-'
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\'\\"-)' ]] || err_exit $M
x=$'-(-\\"\')\\\'\\"-)-'
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'(-\\"\')\\\'\\"-)' ]] || err_exit $M
x=$'-(-\\"\')\\\'\\"\'-)-'
[[ ${x/*%(()E\\L\'Q\")*/\1} == $'-(-\\"\')\\\'\\"\'-)-' ]] || err_exit $M
pattern=00
var=100
[[ $( print $(( ${var%%$pattern} )) ) == 1 ]] || err_exit "arithmetic with embeddded pattern variables fails"
fi
string='foo(d:\nt\box\something)bar'
expected='d:\nt\box\something'
[[ ${string/*\(+([!\)])\)*/\1} == "$expected" ]] || err_exit "substring expansion failed '${string/*\(+([!\)])\)*/\1}' returned -- '$expected' expected"
if [[ $($SHELL -c $'export LC_ALL=C.UTF-8; print -r "\342\202\254\342\202\254\342\202\254\342\202\254w\342\202\254\342\202\254\342\202\254\342\202\254" | wc -m' 2>/dev/null) == 10 ]]
then LC_ALL=C.UTF-8 $SHELL -c b1=$'"\342\202\254\342\202\254\342\202\254\342\202\254w\342\202\254\342\202\254\342\202\254\342\202\254"; [[ ${b1:4:1} == w ]]' || err_exit 'multibyte ${var:offset:len} not working correctly'
fi
{ $SHELL -c 'unset x;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is not set'
{ $SHELL -c 'x=;[[ ${SHELL:$x} == $SHELL ]]';} 2> /dev/null || err_exit '${var:$x} fails when x is null'
# subject mode pattern result #
set -- \
'a$z' 'E' '[$]|#' 'a($)z' \
'a#z' 'E' '[$]|#' 'a(#)z' \
'a$z' 'Elr' '[$]|#' 'a$z' \
'a#z' 'Elr' '[$]|#' 'a#z' \
'a$' 'E' '[$]|#' 'a($)' \
'a#' 'E' '[$]|#' 'a(#)' \
'a$' 'Elr' '[$]|#' 'a$' \
'a#' 'Elr' '[$]|#' 'a#' \
'$z' 'E' '[$]|#' '($)z' \
'#z' 'E' '[$]|#' '(#)z' \
'$z' 'Elr' '[$]|#' '$z' \
'#z' 'Elr' '[$]|#' '#z' \
'$' 'E' '[$]|#' '($)' \
'#' 'E' '[$]|#' '(#)' \
'$' 'Elr' '[$]|#' '($)' \
'#' 'Elr' '[$]|#' '(#)' \
'a$z' 'E' '\$|#' 'a$z()' \
'a$z' 'E' '\\$|#' 'a$z' \
'a$z' 'E' '\\\$|#' 'a($)z' \
'a#z' 'E' '\\\$|#' 'a(#)z' \
'a$z' 'Elr' '\\\$|#' 'a$z' \
'a#z' 'Elr' '\\\$|#' 'a#z' \
'a$' 'E' '\\\$|#' 'a($)' \
'a#' 'E' '\\\$|#' 'a(#)' \
'a$' 'Elr' '\\\$|#' 'a$' \
'a#' 'Elr' '\\\$|#' 'a#' \
'$z' 'E' '\\\$|#' '($)z' \
'#z' 'E' '\\\$|#' '(#)z' \
'$z' 'Elr' '\\\$|#' '$z' \
'#z' 'Elr' '\\\$|#' '#z' \
'$' 'E' '\\\$|#' '($)' \
'#' 'E' '\\\$|#' '(#)' \
'$' 'Elr' '\\\$|#' '($)' \
'#' 'Elr' '\\\$|#' '(#)' \
# do not delete this line #
unset i o
while (( $# >= 4 ))
do i=$1
eval o="\${i/~($2)$3/\\(\\0\\)}"
if [[ "$o" != "$4" ]]
fi
eval o="\${i/~($2)($3)/\\(\\1\\)}"
if [[ "$o" != "$4" ]]
fi
shift 4
done
#multibyte locale tests
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:1}" == "<2b|>" || err_exit ${x:1:1} should be <2b|>'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:3:1}" == "<3d|\\>" || err_exit ${x:3:1} should be <3d|\>'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1}" == "<2b|>c<3d|\\>e" || print -u2 ${x:1}" should be <2b|>c<3d|\>e'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x: -1:1}" == e || err_exit ${x: -1:1} should be e'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x: -2:1}" == "<3d|\\>" || err_exit ${x: -2:1} == <3d|\>'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:3}" == "<2b|>c<3d|\\>" || err_exit ${x:1:3} should be <2b|>c<3d|\>'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x:1:20}" == "<2b|>c<3d|\\>e" || err_exit ${x:1:20} should be <2b|>c<3d|\>e'
x='a<2b|>c<3d|\>e' LC_ALL=debug $SHELL -c 'test "${x#??}" == "c<3d|\\>e" || err_exit "${x#??} should be c<3d|\>e'
x='a one and a two'
{
$SHELL -c 'typeset x="123" ; integer i=100 ; print -n "${x:i:5}"'
A='|'
x="111 222 333 444 555 666"
[[ $x == ~(E)(...).(...).(...) ]]
x="foo bar"
dummy=${x/~(E)(*)/}
[[ ${ print -v .sh.match;} ]] && err_exit 'print -v should show .sh.match empty when there are no matches'
then set 1 2 3 4 5 6 7 8 9 10 11 12
[[ ${##[0-9]} == 2 ]] || err_exit '${##[0-9]} should be 2 with $#==12'
[[ ${###[0-9]} == 2 ]] || err_exit '${###[0-9]} should be 2 with $#==12'
[[ ${#%[0-9]} == 1 ]] || err_exit '${#%[0-9]} should be 1 with $#==12'
[[ ${#%%[0-9]} == 1 ]] || err_exit '${#%%[0-9]} should be 1 with $#==12'
else err_exit '${##[0-9]} give syntax error'
fi
{
sleep .5; kill $!; wait $!
function foo
{
typeset x="123 456 789 abc"
typeset dummy="${x/~(E-g)([[:digit:]][[:digit:]])((X)|([[:digit:]]))([[:blank:]])/_}"
exp=$'(\n\t[0]=\'123 \'\n\t[1]=12\n\t[2]=3\n\t[4]=3\n\t[5]=\' \'\n)'
}
x="a 1 b"
[[ $(print -v .sh.match) == $'(\n\t[0]=a\n\t[1]=a\n\t[3]=a\n)' ]] || err_exit '.sh.match not sparse'
unset v
typeset -a arr=( 0 1 2 3 4 )
for v in "${arr[@]:5}"
do err_exit "\${arr[@]:5} should not generate $v"
break
done
for v in "${arr[@]:1:0}"
do err_exit "\${arr[@]:1:0} should not generate ${v:-empty_string}"
break
done
for v in "${arr[@]:0:-1}"
do err_exit "\${arr[@]:0:-1} should not generate ${v:-empty_string}"
break
done
set 1 2 3 4
for v in "${@:5}"
do err_exit "\${@:5} should not generate $v"
break
done
for v in "${@:1:0}"
do err_exit "\${@:1:0} should not generate ${v:-empty_string}"
break
done
for v in "${@:0:-1}"
do err_exit "\${@:0:-1} should not generate ${v:-empty_string}"
break
done
unset v d
v=abbbc
d="${v/~(E)b{2,4}/dummy}"