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 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; exit 3; }
43 echo ${PIPESTATUS[@]}
44 ## stdout: 1 2 3
45 ## N-I dash status: 2
46 ## N-I zsh status: 3
47 ## N-I dash/zsh stdout-json: ""
48
49 #### |&
50 stdout_stderr.py |& cat
51 ## stdout-json: "STDERR\nSTDOUT\n"
52 ## status: 0
53 ## N-I dash/mksh stdout-json: ""
54 ## N-I dash status: 2
55
56 #### ! turns non-zero into zero
57 ! $SH -c 'exit 42'; echo $?
58 ## stdout: 0
59 ## status: 0
60
61 #### ! turns zero into 1
62 ! $SH -c 'exit 0'; echo $?
63 ## stdout: 1
64 ## status: 0
65
66 #### ! in if
67 if ! echo hi; then
68 echo TRUE
69 else
70 echo FALSE
71 fi
72 ## stdout-json: "hi\nFALSE\n"
73 ## status: 0
74
75 #### ! with ||
76 ! echo hi || echo FAILED
77 ## stdout-json: "hi\nFAILED\n"
78 ## status: 0
79
80 #### ! with { }
81 ! { echo 1; echo 2; } || echo FAILED
82 ## stdout-json: "1\n2\nFAILED\n"
83 ## status: 0
84
85 #### ! with ( )
86 ! ( echo 1; echo 2 ) || echo FAILED
87 ## stdout-json: "1\n2\nFAILED\n"
88 ## status: 0
89
90 #### ! is not a command
91 v='!'
92 $v echo hi
93 ## status: 127
94
95 #### Evaluation of argv[0] in pipeline occurs in child
96 ${cmd=echo} hi | wc -l
97 echo "cmd=$cmd"
98 ## stdout-json: "1\ncmd=\n"
99 ## BUG zsh stdout-json: "1\ncmd=echo\n"
100
101 #### last command is run in its own process
102 echo hi | read line
103 echo "line=$line"
104 ## stdout: line=
105 ## OK zsh stdout: line=hi
106
107 #### shopt -s lastpipe
108 shopt -s lastpipe
109 echo hi | read line
110 echo "line=$line"
111 ## stdout: line=hi
112 ## N-I dash/mksh stdout: line=
113
114 #### shopt -s lastpipe
115 shopt -s lastpipe
116 i=0
117 seq 3 | while read line; do
118 (( i++ ))
119 done
120 echo i=$i
121 ## stdout: i=3
122 ## N-I dash/mksh stdout: i=0
123