1 #!/bin/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 in pipeline
14 for w in one two; do
15 echo $w
16 done | tac
17 # stdout-json: "two\none\n"
18
19 ### Redirect in Pipeline
20 echo hi 1>&2 | wc -l
21 # stdout: 0
22 # BUG zsh stdout: 1
23
24 ### Exit code is last status
25 echo a | egrep '[0-9]+'
26 # status: 1
27
28 ### PIPESTATUS
29 { sleep 0.03; exit 1; } | { sleep 0.02; exit 2; } | { sleep 0.01; exit 3; }
30 echo ${PIPESTATUS[@]}
31 # stdout: 1 2 3
32 # N-I dash status: 2
33 # N-I zsh status: 3
34 # N-I dash/zsh stdout-json: ""
35
36 ### |&
37 stdout_stderr.py |& cat
38 # stdout-json: "STDERR\nSTDOUT\n"
39 # status: 0
40 # N-I dash/mksh stdout-json: ""
41 # N-I dash status: 2
42
43 ### ! turns non-zero into zero
44 ! $SH -c 'exit 42'; echo $?
45 # stdout: 0
46 # status: 0
47
48 ### ! turns zero into 1
49 ! $SH -c 'exit 0'; echo $?
50 # stdout: 1
51 # status: 0
52
53 ### ! in if
54 if ! echo hi; then
55 echo TRUE
56 else
57 echo FALSE
58 fi
59 # stdout-json: "hi\nFALSE\n"
60 # status: 0
61
62 ### ! with ||
63 ! echo hi || echo FAILED
64 # stdout-json: "hi\nFAILED\n"
65 # status: 0
66
67 ### ! with { }
68 ! { echo 1; echo 2; } || echo FAILED
69 # stdout-json: "1\n2\nFAILED\n"
70 # status: 0
71
72 ### ! with ( )
73 ! ( echo 1; echo 2 ) || echo FAILED
74 # stdout-json: "1\n2\nFAILED\n"
75 # status: 0
76
77 ### ! is not a command
78 v='!'
79 $v echo hi
80 # status: 127
81
82 ### Evaluation of argv[0] in pipeline occurs in child
83 ${cmd=echo} hi | wc -l
84 echo "cmd=$cmd"
85 # stdout-json: "1\ncmd=\n"
86 # BUG zsh stdout-json: "1\ncmd=echo\n"