OILS / demo / 01-async.sh View on Github | oilshell.org

39 lines, 22 significant
1#!/usr/bin/env bash
2#
3# Testing compound statements with &.
4
5# NOTE: This has a very different process pattern under dash than bash!
6
7#set -o nounset
8#set -o pipefail
9set -o errexit
10
11sleep2() {
12 echo one
13 sleep 0.5
14 echo two
15 sleep 0.5
16}
17
18proc_tree() {
19 local pid=$1
20
21 sleep 0.1 # wait for others to start
22 pstree --compact --ascii $pid
23}
24
25main() {
26 # A whole AND_OR can be async
27 local pid=$$
28
29 sleep2 && echo DONE &
30 proc_tree $pid # shows forking ONE shell
31 wait
32
33 # Pipeline
34 sleep2 | rev && echo DONE &
35 proc_tree $pid # shows forking TWO shells
36 wait
37}
38
39main "$@"