OILS / demo / process-sub.sh View on Github | oilshell.org

69 lines, 35 significant
1#!/usr/bin/env bash
2#
3# I think this is a zsh feature ported to bash.
4#
5# zsh gives /proc/self/fd/12, while bash gives /dev/fd/63
6#
7# Usage:
8# demo/process-sub.sh <function name>
9
10set -o nounset
11set -o pipefail
12set -o errexit
13
14stdout() {
15 cat <(seq 2) <(seq 3)
16
17 echo ---
18 # Is it possible to pick up this failure?
19 # Nope it gets lost
20 cat <(seq 2) <(seq ZZ)
21
22 echo ---
23 echo pipestatus=${PIPESTATUS[@]}
24}
25
26stdin() {
27 # this one hangs a little in bash
28 seq 3 > >(tac)
29}
30
31stdin-shell() {
32 # key difference: the SHELL ITSELF is writing to the pipe, not a forked
33 # process like 'seq'
34
35 echo $'1\n2\n3\n' > >(tac)
36}
37
38stdin-shell-2() {
39 #{ echo 4; echo 5; echo 6; } > >(tac)
40
41 echo "pid = $$"
42
43 echo '__ ONE ___'
44 echo 99 > >(tac)
45
46 # This used to hang!
47 echo '__ ONE ___'
48 { echo 99; } > >(tac)
49
50 echo '__ TWO ___'
51 { echo 4; echo 5; } > >(tac)
52}
53
54both() {
55 diff -u <(seq 2) <(seq 3) > >(tac) || true
56
57 if test -n "${OIL_VERSION:-}"; then
58 echo status=${_process_sub_status[@]}
59 fi
60
61 diff -u <(seq 2; exit 2) <(seq 3; exit 3) > >(tac; exit 5) || true
62
63 if test -n "${OIL_VERSION:-}"; then
64 echo status=${_process_sub_status[@]}
65 fi
66}
67
68
69"$@"