1 #!/usr/bin/env bash
2 #
3 # Tests for pipelines.
4 # NOTE: Grammatically, ! is part of the pipeline:
5 #
6 # pipeline : pipe_sequence
7 # | Bang pipe_sequence
8
9 #### Brace group in pipeline
10 { echo one; echo two; } | tac
11 ## stdout-json: "two\none\n"
12
13 #### For loop starts pipeline
14 for w in one two; do
15 echo $w
16 done | tac
17 ## stdout-json: "two\none\n"
18
19 #### While Loop ends pipeline
20 seq 3 | while read i
21 do
22 echo ".$i"
23 done
24 ## stdout-json: ".1\n.2\n.3\n"
25
26 #### Redirect in Pipeline
27 echo hi 1>&2 | wc -l
28 ## stdout: 0
29 ## BUG zsh stdout: 1
30
31 #### Pipeline comments
32 echo abcd | # input
33 # blank line
34 tr a-z A-Z # transform
35 ## stdout: ABCD
36
37 #### Exit code is last status
38 echo a | egrep '[0-9]+'
39 ## status: 1
40
41 #### PIPESTATUS
42 return3() {
43 return 3
44 }
45 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
46 echo ${PIPESTATUS[@]}
47 ## stdout: 1 2 3
48 ## N-I dash status: 2
49 ## N-I dash stdout-json: ""
50 ## N-I zsh status: 0
51 ## N-I zsh stdout-json: "\n"
52
53 #### PIPESTATUS with shopt -s lastpipe
54 shopt -s lastpipe
55 return3() {
56 return 3
57 }
58 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; return3; }
59 echo ${PIPESTATUS[@]}
60 ## stdout: 1 2 3
61 ## N-I dash status: 2
62 ## N-I dash stdout-json: ""
63 ## N-I zsh status: 0
64 ## N-I zsh stdout-json: "\n"
65
66 #### |&
67 stdout_stderr.py |& cat
68 ## stdout-json: "STDERR\nSTDOUT\n"
69 ## status: 0
70 ## N-I dash/mksh stdout-json: ""
71 ## N-I dash status: 2
72
73 #### ! turns non-zero into zero
74 ! $SH -c 'exit 42'; echo $?
75 ## stdout: 0
76 ## status: 0
77
78 #### ! turns zero into 1
79 ! $SH -c 'exit 0'; echo $?
80 ## stdout: 1
81 ## status: 0
82
83 #### ! in if
84 if ! echo hi; then
85 echo TRUE
86 else
87 echo FALSE
88 fi
89 ## stdout-json: "hi\nFALSE\n"
90 ## status: 0
91
92 #### ! with ||
93 ! echo hi || echo FAILED
94 ## stdout-json: "hi\nFAILED\n"
95 ## status: 0
96
97 #### ! with { }
98 ! { echo 1; echo 2; } || echo FAILED
99 ## stdout-json: "1\n2\nFAILED\n"
100 ## status: 0
101
102 #### ! with ( )
103 ! ( echo 1; echo 2 ) || echo FAILED
104 ## stdout-json: "1\n2\nFAILED\n"
105 ## status: 0
106
107 #### ! is not a command
108 v='!'
109 $v echo hi
110 ## status: 127
111
112 #### Evaluation of argv[0] in pipeline occurs in child
113 ${cmd=echo} hi | wc -l
114 echo "cmd=$cmd"
115 ## STDOUT:
116 1
117 cmd=
118 ## END
119 ## BUG zsh STDOUT:
120 1
121 cmd=echo
122 ## END
123
124 #### bash/dash/mksh run the last command is run in its own process
125 echo hi | read line
126 echo "line=$line"
127 ## stdout: line=hi
128 ## OK bash/dash/mksh stdout: line=
129
130 #### shopt -s lastpipe (always on in OSH)
131 shopt -s lastpipe
132 echo hi | read line
133 echo "line=$line"
134 ## stdout: line=hi
135 ## N-I dash/mksh stdout: line=
136
137 #### shopt -s lastpipe (always on in OSH)
138 shopt -s lastpipe
139 i=0
140 seq 3 | while read line; do
141 (( i++ ))
142 done
143 echo i=$i
144 ## stdout: i=3
145 ## N-I dash/mksh stdout: i=0
146