1 ## oils_failures_allowed: 0
2 ## compare_shells: bash dash mksh zsh
3
4 #
5 # Tests for pipelines.
6 # NOTE: Grammatically, ! is part of the pipeline:
7 #
8 # pipeline : pipe_sequence
9 # | Bang pipe_sequence
10
11 #### Brace group in pipeline
12 { echo one; echo two; } | tac
13 ## stdout-json: "two\none\n"
14
15 #### For loop starts pipeline
16 for w in one two; do
17 echo $w
18 done | tac
19 ## stdout-json: "two\none\n"
20
21 #### While Loop ends pipeline
22 seq 3 | while read i
23 do
24 echo ".$i"
25 done
26 ## stdout-json: ".1\n.2\n.3\n"
27
28 #### Redirect in Pipeline
29 echo hi 1>&2 | wc -l
30 ## stdout: 0
31 ## BUG zsh stdout: 1
32
33 #### Pipeline comments
34 echo abcd | # input
35 # blank line
36 tr a-z A-Z # transform
37 ## stdout: ABCD
38
39 #### Exit code is last status
40 echo a | egrep '[0-9]+'
41 ## status: 1
42
43 #### PIPESTATUS
44 return3() {
45 return 3
46 }
47 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
48 echo ${PIPESTATUS[@]}
49 ## stdout: 1 2 3
50 ## N-I dash status: 2
51 ## N-I dash stdout-json: ""
52 ## N-I zsh status: 0
53 ## N-I zsh stdout-json: "\n"
54
55 #### PIPESTATUS is set on simple commands, but NOT in OSH
56 case $SH in dash|zsh) exit ;; esac
57
58 false
59 echo pipestatus ${PIPESTATUS[@]}
60
61 ## STDOUT:
62 pipestatus 1
63 ## END
64 ## OK osh STDOUT:
65 pipestatus
66 ## END
67 ## N-I dash/zsh STDOUT:
68 ## END
69
70 #### PIPESTATUS with shopt -s lastpipe
71 shopt -s lastpipe
72 return3() {
73 return 3
74 }
75 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
76 echo ${PIPESTATUS[@]}
77 ## stdout: 1 2 3
78 ## N-I dash status: 2
79 ## N-I dash stdout-json: ""
80 ## N-I zsh status: 0
81 ## N-I zsh stdout-json: "\n"
82
83 #### |&
84 stdout_stderr.py |& cat
85 ## STDOUT:
86 STDERR
87 STDOUT
88 ## END
89 ## status: 0
90 ## N-I dash/mksh stdout-json: ""
91 ## N-I dash status: 2
92 ## N-I osh stdout-json: ""
93 ## N-I osh status: 1
94
95 #### ! turns non-zero into zero
96 ! $SH -c 'exit 42'; echo $?
97 ## stdout: 0
98 ## status: 0
99
100 #### ! turns zero into 1
101 ! $SH -c 'exit 0'; echo $?
102 ## stdout: 1
103 ## status: 0
104
105 #### ! in if
106 if ! echo hi; then
107 echo TRUE
108 else
109 echo FALSE
110 fi
111 ## stdout-json: "hi\nFALSE\n"
112 ## status: 0
113
114 #### ! with ||
115 ! echo hi || echo FAILED
116 ## stdout-json: "hi\nFAILED\n"
117 ## status: 0
118
119 #### ! with { }
120 ! { echo 1; echo 2; } || echo FAILED
121 ## stdout-json: "1\n2\nFAILED\n"
122 ## status: 0
123
124 #### ! with ( )
125 ! ( echo 1; echo 2 ) || echo FAILED
126 ## stdout-json: "1\n2\nFAILED\n"
127 ## status: 0
128
129 #### ! is not a command
130 v='!'
131 $v echo hi
132 ## status: 127
133
134 #### Evaluation of argv[0] in pipeline occurs in child
135 ${cmd=echo} hi | wc -l
136 echo "cmd=$cmd"
137 ## STDOUT:
138 1
139 cmd=
140 ## END
141 ## BUG zsh STDOUT:
142 1
143 cmd=echo
144 ## END
145
146 #### bash/dash/mksh run the last command is run in its own process
147 echo hi | read line
148 echo "line=$line"
149 ## stdout: line=hi
150 ## OK bash/dash/mksh stdout: line=
151
152 #### shopt -s lastpipe (always on in OSH)
153 shopt -s lastpipe
154 echo hi | read line
155 echo "line=$line"
156 ## stdout: line=hi
157 ## N-I dash/mksh stdout: line=
158
159 #### shopt -s lastpipe (always on in OSH)
160 shopt -s lastpipe
161 i=0
162 seq 3 | while read line; do
163 (( i++ ))
164 done
165 echo i=$i
166 ## stdout: i=3
167 ## N-I dash/mksh stdout: i=0
168
169
170 #### SIGPIPE causes pipeline to die (regression for issue #295)
171 cat /dev/urandom | sleep 0.1
172 echo ${PIPESTATUS[@]}
173
174 # hm bash gives '1 0' which seems wrong
175
176 ## STDOUT:
177 141 0
178 ## END
179 ## BUG bash STDOUT:
180 1 0
181 ## END
182 ## N-I zsh stdout:
183 ## N-I dash status: 2
184 ## N-I dash stdout-json: ""
185
186 #### Nested pipelines
187 { sleep 0.1 | seq 3; } | cat
188 { sleep 0.1 | seq 10; } | { cat | cat; } | wc -l
189 ## STDOUT:
190 1
191 2
192 3
193 10
194 ## END
195
196 #### Pipeline in eval
197 ls /dev/null | eval 'cat | cat' | wc -l
198 ## STDOUT:
199 1
200 ## END
201
202
203 #### shopt -s lastpipe and shopt -s no_last_fork interaction
204
205 case $SH in dash) exit ;; esac
206
207 $SH -c '
208 shopt -s lastpipe
209 set -o errexit
210 set -o pipefail
211
212 ls | false | wc -l'
213 echo status=$?
214
215 # Why does this give status 0? It should fail
216
217 $SH -c '
218 shopt -s lastpipe
219 shopt -s no_fork_last # OSH only
220 set -o errexit
221 set -o pipefail
222
223 ls | false | wc -l'
224 echo status=$?
225
226 ## STDOUT:
227 0
228 status=1
229 0
230 status=1
231 ## END
232
233 ## N-I dash STDOUT:
234 ## END