1 #### var ref to array 'preHooks[@]'
2 #
3 # This idiom discussed on
4 # https://github.com/NixOS/nixpkgs/pull/147629
5
6 show() {
7 echo show
8
9 # These are actually different
10 argv.py ${!hooksSlice}
11
12 argv.py ${!hooksSlice+"${!hooksSlice}"}
13 }
14
15 hooksSlice='preHooks[@]'
16
17 preHooks=()
18 show
19
20 preHooks=('foo bar' baz)
21 show
22
23 # WTF this exposes a difference? But not the test case below?
24
25 # What's happening here?
26 # Uncomment this and get an error in bash about hookSlice, even though we never
27 # undefined it.
28
29 #wtf=1
30 #
31 # line 6: !hooksSlice: unbound variable
32
33 if test -n "$wtf"; then
34 # 4.4.0(1)-release
35 # echo $BASH_VERSION
36
37 set -u
38 preHooks=()
39 show
40
41 preHooks=('foo bar' baz)
42 show
43 fi
44
45 ## STDOUT:
46 show
47 []
48 []
49 show
50 ['foo', 'bar', 'baz']
51 ['foo bar', 'baz']
52 ## END
53
54 #### Same as above with set -u
55 show() {
56 echo show
57
58 # bash gives an error here - !hookSlice unbound, even though preHooks exists
59 # OSH currently does the "logical" thing
60 argv.py ${!hooksSlice}
61
62 argv.py ${!hooksSlice+"${!hooksSlice}"}
63 }
64
65 hooksSlice='preHooks[@]'
66
67 set -u
68 preHooks=()
69 show
70
71 preHooks=('foo bar' baz)
72 show
73
74 ## STDOUT:
75 show
76 ## END
77 ## status: 1
78
79
80 #### ${!ref} to undefined array
81
82 set -u
83 shopt -s eval_unsafe_arith || true 2>/dev/null
84
85 #preHooks=()
86 hookSlice="preHooks[@]"
87
88 argv.py ${!hookSlice+"${!hookSlice}"}
89
90 for element in ${!hookSlice+"${!hookSlice}"}; do
91 echo $element
92 done
93
94 ## STDOUT:
95 []
96 ## END
97
98 #### ${!ref} to undefined string var is fatal, INCONSISTENT with array
99 hookSlice='preHooks'
100
101 argv.py ${!hookSlice}
102
103 set -u
104
105 argv.py ${!hookSlice}
106
107 echo end
108
109 ## status: 1
110 ## STDOUT:
111 []
112 ## END
113
114 #### export with dynamic var name +=
115
116 orig() {
117 export NIX_LDFLAGS${role_post}+=" -L$1/lib64"
118 }
119
120 new() {
121 local var_name="NIX_LDFLAGS$role_post"
122 local value=" -L$1/lib64"
123
124 eval "$var_name"+='$value'
125 export "$var_name"
126 }
127
128 role_post='_foo'
129
130 # set -u
131
132 if test -n "${BASH_VERSION:-}"; then
133 orig one
134 fi
135
136 declare -p NIX_LDFLAGS_foo # inspect it
137 unset NIX_LDFLAGS_foo
138
139 new one
140
141 declare -p NIX_LDFLAGS_foo # inspect it
142
143 ## STDOUT:
144 declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
145 declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
146 ## END
147 ## OK osh STDOUT:
148 declare -x NIX_LDFLAGS_foo=' -Lone/lib64'
149 ## END
150