OILS / spec / builtin-bash.test.sh View on Github | oils.pub

215 lines, 129 significant
1## oils_failures_allowed: 4
2## compare_shells: bash
3
4
5#### help
6help
7echo status=$? >&2
8help help
9echo status=$? >&2
10help -- help
11echo status=$? >&2
12## STDERR:
13status=0
14status=0
15status=0
16## END
17
18#### bad help topic
19help ZZZ 2>$TMP/err.txt
20echo "help=$?"
21cat $TMP/err.txt | grep -i 'no help topics' >/dev/null
22echo "grep=$?"
23## STDOUT:
24help=1
25grep=0
26## END
27
28#### mapfile
29type mapfile >/dev/null 2>&1 || exit 0
30printf '%s\n' {1..5..2} | {
31 mapfile
32 echo "n=${#MAPFILE[@]}"
33 printf '[%s]\n' "${MAPFILE[@]}"
34}
35## STDOUT:
36n=3
37[1
38]
39[3
40]
41[5
42]
43## END
44## N-I dash/mksh/zsh/ash STDOUT:
45## END
46
47#### readarray (synonym for mapfile)
48type readarray >/dev/null 2>&1 || exit 0
49printf '%s\n' {1..5..2} | {
50 readarray
51 echo "n=${#MAPFILE[@]}"
52 printf '[%s]\n' "${MAPFILE[@]}"
53}
54## STDOUT:
55n=3
56[1
57]
58[3
59]
60[5
61]
62## END
63## N-I dash/mksh/zsh/ash STDOUT:
64## END
65
66#### mapfile (array name): arr
67type mapfile >/dev/null 2>&1 || exit 0
68printf '%s\n' {1..5..2} | {
69 mapfile arr
70 echo "n=${#arr[@]}"
71 printf '[%s]\n' "${arr[@]}"
72}
73## STDOUT:
74n=3
75[1
76]
77[3
78]
79[5
80]
81## END
82## N-I dash/mksh/zsh/ash STDOUT:
83## END
84
85#### mapfile (delimiter): -d delim
86# Note: Bash-4.4+
87type mapfile >/dev/null 2>&1 || exit 0
88printf '%s:' {1..5..2} | {
89 mapfile -d : arr
90 echo "n=${#arr[@]}"
91 printf '[%s]\n' "${arr[@]}"
92}
93## STDOUT:
94n=3
95[1:]
96[3:]
97[5:]
98## END
99## N-I dash/mksh/zsh/ash STDOUT:
100## END
101
102#### mapfile (delimiter): -d '' (null-separated)
103# Note: Bash-4.4+
104type mapfile >/dev/null 2>&1 || exit 0
105printf '%s\0' {1..5..2} | {
106 mapfile -d '' arr
107 echo "n=${#arr[@]}"
108 printf '[%s]\n' "${arr[@]}"
109}
110## STDOUT:
111n=3
112[1]
113[3]
114[5]
115## END
116## N-I dash/mksh/zsh/ash STDOUT:
117## END
118
119#### mapfile (truncate delim): -t
120type mapfile >/dev/null 2>&1 || exit 0
121printf '%s\n' {1..5..2} | {
122 mapfile -t arr
123 echo "n=${#arr[@]}"
124 printf '[%s]\n' "${arr[@]}"
125}
126## STDOUT:
127n=3
128[1]
129[3]
130[5]
131## END
132## N-I dash/mksh/zsh/ash STDOUT:
133## END
134
135#### mapfile -t doesn't remove \r
136type mapfile >/dev/null 2>&1 || exit 0
137printf '%s\r\n' {1..5..2} | {
138 mapfile -t arr
139 argv.py "${arr[@]}"
140}
141## STDOUT:
142['1\r', '3\r', '5\r']
143## END
144## N-I dash/mksh/zsh/ash STDOUT:
145## END
146
147#### mapfile -t bugs (ble.sh)
148
149# empty line
150mapfile -t lines <<< $'hello\n\nworld'
151echo len=${#lines[@]}
152#declare -p lines
153
154# initial newline
155mapfile -t lines <<< $'\nhello'
156echo len=${#lines[@]}
157#declare -p lines
158
159# trailing newline
160mapfile -t lines <<< $'hello\n'
161echo len=${#lines[@]}
162#declare -p lines
163
164## STDOUT:
165len=3
166len=2
167len=2
168## END
169
170#### mapfile (store position): -O start
171type mapfile >/dev/null 2>&1 || exit 0
172printf '%s\n' a{0..2} | {
173 arr=(x y z)
174 mapfile -O 2 -t arr
175 echo "n=${#arr[@]}"
176 printf '[%s]\n' "${arr[@]}"
177}
178## STDOUT:
179n=5
180[x]
181[y]
182[a0]
183[a1]
184[a2]
185## END
186## N-I dash/mksh/zsh/ash STDOUT:
187## END
188
189#### mapfile (input range): -s start -n count
190type mapfile >/dev/null 2>&1 || exit 0
191printf '%s\n' a{0..10} | {
192 mapfile -s 5 -n 3 -t arr
193 echo "n=${#arr[@]}"
194 printf '[%s]\n' "${arr[@]}"
195}
196## STDOUT:
197n=3
198[a5]
199[a6]
200[a7]
201## END
202## N-I dash/mksh/zsh/ash STDOUT:
203## END
204
205#### mapfile / readarray stdin TODO: Fix me.
206shopt -s lastpipe # for bash
207
208seq 2 | mapfile m
209seq 3 | readarray r
210echo ${#m[@]}
211echo ${#r[@]}
212## STDOUT:
2132
2143
215## END