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