extend assoc_expand_once to indexed arrays; rename to array_expand_once, keeping old name as a synonym; udpate shopt documentation

This commit is contained in:
Chet Ramey
2023-06-13 11:44:33 -04:00
parent d44a45afbc
commit 76112093ec
35 changed files with 1389 additions and 1157 deletions
+29
View File
@@ -793,3 +793,32 @@ declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
declare -a aa=([0]="/homes/cj/Desktop:/homes/cj/Library:/homes/cj/Documents" [1]="/homes/cj/Applications")
./array32.sub: line 7: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 8: declare: a: not found
./array32.sub: line 11: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 12: declare: a: not found
./array32.sub: line 16: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a=([0]="hi")
./array32.sub: line 22: declare: a: not found
./array32.sub: line 25: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 26: declare: a: not found
./array32.sub: line 29: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a
./array32.sub: line 37: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a
./array32.sub: line 44: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -ai a
./array32.sub: line 52: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 53: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 55: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 62: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a
./array32.sub: line 64: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a
./array32.sub: line 67: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a
./array32.sub: line 72: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
./array32.sub: line 78: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a=()
./array32.sub: line 82: $(echo INJECTION! >&2 ; echo 0): arithmetic syntax error: operand expected (error token is "$(echo INJECTION! >&2 ; echo 0)")
declare -a a=()
+1
View File
@@ -428,3 +428,4 @@ ${THIS_SH} ./array28.sub
${THIS_SH} ./array29.sub
${THIS_SH} ./array30.sub
${THIS_SH} ./array31.sub
${THIS_SH} ./array32.sub
+86
View File
@@ -0,0 +1,86 @@
# change behavior of shell builtins to extend assoc_expand_once to indexed
# arrays
export subscript='$(echo INJECTION! >&2 ; echo 0)'
shopt -s array_expand_once
printf -v a["$subscript"] %s hi
declare -p a
unset a
printf -v "a[$subscript]" %s hi
declare -p a
a[0]=hi
unset a["$subscript"]
declare -p a
unset a
unset a["$subscript"]
declare -p a
unset -v a
read a["$subscript"] <<<hi
declare -p a
declare -a a
read a["$subscript"] <<<hi
declare -p a
unset -v a
declare -a a
{ sleep 1; exit 12; } & bgpid=$!
wait -n -p a["$subscript"] $bgpid
declare -p a
unset -v a
declare -a a
declare -i a["$subscript"]=42
declare -p a
# this still won't work because the quotes prevent it from being recognized as
# an assignment statement
#declare -i "a[$subscript]"=42
#declare -p a
test -v a["$subscript"] && echo set
[ -v a["$subscript"] ] && echo set
let a["$subscript"]+=1
unset -v a
# these are all already arithmetic expression errors
declare -a a
(( a[$subscript]++ ))
declare -p a
: $(( a[$subscript]++ ))
declare -p a
a[$subscript]=hi
declare -p a
# length shortcuts for unset variables, so give it a value
a[0]=zero
echo ${#a[$subscript]}
unset -v a
# compound assignments should not perform double expansion
a=( [$subscript]=hi )
declare -p a
declare -a a
a=( [$subscript]=hi )
declare -p a
unset -v a
+3
View File
@@ -2,6 +2,7 @@
shopt: usage: shopt [-pqsu] [-o] [optname ...]
--
shopt -u autocd
shopt -u array_expand_once
shopt -u assoc_expand_once
shopt -u cdable_vars
shopt -s cdspell
@@ -79,6 +80,7 @@ shopt -s promptvars
shopt -s sourcepath
--
shopt -u autocd
shopt -u array_expand_once
shopt -u assoc_expand_once
shopt -u cdable_vars
shopt -u checkhash
@@ -123,6 +125,7 @@ shopt -u varredir_close
shopt -u xpg_echo
--
autocd off
array_expand_once off
assoc_expand_once off
cdable_vars off
checkhash off