-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdemo.sh
executable file
·108 lines (85 loc) · 2 KB
/
demo.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#!/bin/bash
#
# Usage:
# ./demo.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
compare() {
diff -u "$@" && echo 'PASSED'
}
pipe-both() {
mkdir -p _tmp/
local out=_tmp/stdout-stderr.txt
# POSIX way
./stdout_stderr.py 2>&1 | tr A-Z a-z > $out
compare $out - <<EOF
stderr
stdout
EOF
# Special bash |& operator
./stdout_stderr.py |& tr A-Z a-z > $out
compare $out - <<EOF
stderr
stdout
EOF
}
# oil:
# ./stdout_stderr.py |- tr A-Z a-z > $out
pipe-stderr() {
mkdir -p _tmp/
local out=_tmp/stderr.txt
# must dup stderr to stdout first, and then stdout to /dev/null
./stdout_stderr.py 2>&1 >/dev/null | tr A-Z a-z > $out
compare $out - <<EOF
stderr
EOF
}
#oil:
# ./stdout_stderr.py !2 > !1 >/dev/null | tr A-Z a-z > $out
# ./stdout_stderr.py !2 > !1 !1 > /dev/null | tr A-Z a-z > $out
# Maybe:
#
# ./stdout_stderr.py !2 | tr A-Z a-z > $out
#
# But then what happens to stdout?
# http://stackoverflow.com/questions/2342826/how-to-pipe-stderr-and-not-stdout
pipe-stderr-keep-stdout() {
mkdir -p _tmp/
local out=_tmp/stderr.txt
# must dup stderr to stdout first, and then stdout to /dev/null
./stdout_stderr.py 3>&1 1>&2 2>&3 | tr A-Z a-z > $out
compare $out - <<EOF
stderr
EOF
}
process-subs() {
mkdir -p _tmp/
local out=_tmp/stdout.txt
local err=_tmp/stderr.txt
# must dup stderr to stdout first, and then stdout to /dev/null
./stdout_stderr.py > >(tr A-Z a-z > $out) 2> >(tr A-Z a-z > $err)
compare $out - <<EOF
stdout
EOF
compare $err - <<EOF
stderr
EOF
}
# oil:
# ./stdout_stderr.py !1 > $>[tr A-Z a-z > $out] !2 > $>[tr A-Z a-z > $err]
# TODO:
# - Make some graphviz diagrams of the descriptor table?
order-matters() {
echo ---
./stdout_stderr.py >/dev/null 2>&1 # neither on console
echo ---
./stdout_stderr.py 2>&1 >/dev/null # STDERR on console
}
strace-order-matters() {
echo ===
strace -e open,dup2,fcntl sh -c './stdout_stderr.py >/dev/null 2>&1'
echo ===
strace -e open,dup2,fcntl sh -c './stdout_stderr.py 2>&1 >/dev/null'
}
"$@"