1 | ## compare_shells: bash
|
2 | ## oils_failures_allowed: 2
|
3 |
|
4 | #### var ref to array 'preHooks[@]'
|
5 | #
|
6 | # This idiom discussed on
|
7 | # https://github.com/NixOS/nixpkgs/pull/147629
|
8 |
|
9 | show() {
|
10 | echo show
|
11 |
|
12 | # These are actually different
|
13 | argv.py ${!hooksSlice}
|
14 |
|
15 | argv.py ${!hooksSlice+"${!hooksSlice}"}
|
16 | }
|
17 |
|
18 | hooksSlice='preHooks[@]'
|
19 |
|
20 | preHooks=()
|
21 | show
|
22 |
|
23 | preHooks=('foo bar' baz)
|
24 | show
|
25 |
|
26 | # WTF this exposes a difference? But not the test case below?
|
27 |
|
28 | # What's happening here?
|
29 | # Uncomment this and get an error in bash about hookSlice, even though we never
|
30 | # undefined it.
|
31 |
|
32 | #wtf=1
|
33 | #
|
34 | # line 6: !hooksSlice: unbound variable
|
35 |
|
36 | if test -n "$wtf"; then
|
37 | # 4.4.0(1)-release
|
38 | # echo $BASH_VERSION
|
39 |
|
40 | set -u
|
41 | preHooks=()
|
42 | show
|
43 |
|
44 | preHooks=('foo bar' baz)
|
45 | show
|
46 | fi
|
47 |
|
48 | ## STDOUT:
|
49 | show
|
50 | []
|
51 | []
|
52 | show
|
53 | ['foo', 'bar', 'baz']
|
54 | ['foo bar', 'baz']
|
55 | ## END
|
56 |
|
57 | #### Similar to above with set -u
|
58 | show() {
|
59 | echo show
|
60 |
|
61 | # bash gives an error here - !hookSlice unbound, even though preHooks exists
|
62 | # OSH currently does the "logical" thing
|
63 |
|
64 | # NOT testing this -- I think this is WHAT NIX WORKS AROUND WITH
|
65 | #argv.py ${!hooksSlice}
|
66 |
|
67 | argv.py ${!hooksSlice+"${!hooksSlice}"}
|
68 | }
|
69 |
|
70 | hooksSlice='preHooks[@]'
|
71 |
|
72 | set -u
|
73 | preHooks=()
|
74 | show
|
75 |
|
76 | preHooks=('foo bar' baz)
|
77 | show
|
78 |
|
79 | ## STDOUT:
|
80 | show
|
81 | []
|
82 | show
|
83 | ['foo bar', 'baz']
|
84 | ## END
|
85 |
|
86 | #### ${!ref} to undefined array
|
87 |
|
88 | set -u
|
89 | shopt -s eval_unsafe_arith || true 2>/dev/null
|
90 |
|
91 | #preHooks=()
|
92 | hookSlice="preHooks[@]"
|
93 |
|
94 | argv.py ${!hookSlice+"${!hookSlice}"}
|
95 |
|
96 | for element in ${!hookSlice+"${!hookSlice}"}; do
|
97 | echo $element
|
98 | done
|
99 |
|
100 | ## STDOUT:
|
101 | []
|
102 | ## END
|
103 |
|
104 | #### ${!ref} to undefined string var is fatal, INCONSISTENT with array
|
105 | hookSlice='preHooks'
|
106 |
|
107 | argv.py ${!hookSlice}
|
108 |
|
109 | set -u
|
110 |
|
111 | argv.py ${!hookSlice}
|
112 |
|
113 | echo end
|
114 |
|
115 | ## status: 1
|
116 | ## STDOUT:
|
117 | []
|
118 | ## END
|
119 |
|
120 | #### export with dynamic var name +=
|
121 |
|
122 | orig() {
|
123 | export NIX_LDFLAGS${role_post}+=" -L$1/lib64"
|
124 | }
|
125 |
|
126 | new() {
|
127 | local var_name="NIX_LDFLAGS$role_post"
|
128 | local value=" -L$1/lib64"
|
129 |
|
130 | eval "$var_name"+='$value'
|
131 | export "$var_name"
|
132 | }
|
133 |
|
134 | role_post='_foo'
|
135 |
|
136 | # set -u
|
137 |
|
138 | if test -n "${BASH_VERSION:-}"; then
|
139 | orig one
|
140 | fi
|
141 |
|
142 | declare -p NIX_LDFLAGS_foo # inspect it
|
143 | unset NIX_LDFLAGS_foo
|
144 |
|
145 | new one
|
146 |
|
147 | declare -p NIX_LDFLAGS_foo # inspect it
|
148 |
|
149 | ## STDOUT:
|
150 | declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
|
151 | declare -x NIX_LDFLAGS_foo=" -Lone/lib64"
|
152 | ## END
|
153 | ## OK osh STDOUT:
|
154 | declare -x NIX_LDFLAGS_foo=' -Lone/lib64'
|
155 | ## END
|
156 |
|
157 | #### let idiom can be written in POSIX shell - eval ": \$(( ))"
|
158 |
|
159 | for i in 0 1 2; do
|
160 | echo i=$i
|
161 |
|
162 | NIX_ENFORCE_NO_NATIVE=$i
|
163 |
|
164 | outputVar=NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu
|
165 | inputVar=NIX_ENFORCE_NO_NATIVE
|
166 |
|
167 | # Original Nix idiom
|
168 |
|
169 | if test -n "$BASH_VERSION"; then
|
170 | let "${outputVar} |= ${!inputVar:-0}" "1"
|
171 | else
|
172 | # OSH alternative
|
173 | eval ": \$(( ${outputVar} |= ${!inputVar:-0} ))"
|
174 | fi
|
175 |
|
176 | echo NIX_ENFORCE_NO_NATIVE=$NIX_ENFORCE_NO_NATIVE
|
177 | echo NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=$NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu
|
178 | echo
|
179 |
|
180 | done
|
181 |
|
182 |
|
183 | ## STDOUT:
|
184 | i=0
|
185 | NIX_ENFORCE_NO_NATIVE=0
|
186 | NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=0
|
187 |
|
188 | i=1
|
189 | NIX_ENFORCE_NO_NATIVE=1
|
190 | NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=1
|
191 |
|
192 | i=2
|
193 | NIX_ENFORCE_NO_NATIVE=2
|
194 | NIX_ENFORCE_NO_NATIVE_x86_64_unknown_linux_gnu=3
|
195 |
|
196 | ## END
|
197 |
|