OILS / spec / builtin-bash.test.sh View on Github | oilshell.org

192 lines, 120 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 (store position): -O start
148type mapfile >/dev/null 2>&1 || exit 0
149printf '%s\n' a{0..2} | {
150 arr=(x y z)
151 mapfile -O 2 -t arr
152 echo "n=${#arr[@]}"
153 printf '[%s]\n' "${arr[@]}"
154}
155## STDOUT:
156n=5
157[x]
158[y]
159[a0]
160[a1]
161[a2]
162## END
163## N-I dash/mksh/zsh/ash STDOUT:
164## END
165
166#### mapfile (input range): -s start -n count
167type mapfile >/dev/null 2>&1 || exit 0
168printf '%s\n' a{0..10} | {
169 mapfile -s 5 -n 3 -t arr
170 echo "n=${#arr[@]}"
171 printf '[%s]\n' "${arr[@]}"
172}
173## STDOUT:
174n=3
175[a5]
176[a6]
177[a7]
178## END
179## N-I dash/mksh/zsh/ash STDOUT:
180## END
181
182#### mapfile / readarray stdin TODO: Fix me.
183shopt -s lastpipe # for bash
184
185seq 2 | mapfile m
186seq 3 | readarray r
187echo ${#m[@]}
188echo ${#r[@]}
189## STDOUT:
1902
1913
192## END