-
-
Notifications
You must be signed in to change notification settings - Fork 165
/
Copy pathjobs-builtin.sh
executable file
·110 lines (88 loc) · 1.85 KB
/
jobs-builtin.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
109
110
#!/usr/bin/env bash
#
# Compare the output of "jobs" in different shells
#
# Usage:
# $SH demo/jobs-builtin.sh <function name>
set -o nounset
#set -o pipefail
set -o errexit
# Notes:
# - the formats are consistent, because it's specified by POSIX
# - they use [1] [2] [3]
# - + is the default job for 'fg' and 'bg', which is also %%
# - - is the job that would become the default, which is also %-
#
# "[%d] %c %s %s\n", <job-number>, <current>, <state>, <command>
#
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/jobs.html
#
# TODO: YSH should print this as J8! So you can parse it.
#
# - dash is the only shell that doesn't show source code; it just shows | for pipe
# - mksh has some weird quoting, like \sleep 1
# - mksh and zsh doesn't show trailing &, but bash does
#
show_jobs() {
echo ___
# all shells support this long format which shows components of a pipeline
#if true; then
if true; then
jobs -l
else
jobs
fi
}
myfunc() {
sleep 0.3
show_jobs
sleep 0.4
show_jobs
}
demo() {
sleep 1 & sleep 2 &
show_jobs
# In jobs -l, bash, zsh, and mksh all combine this onto one line:
# { echo pipe1; sleep 0.5; }
{ echo pipe1
sleep 0.5
} | tac | wc -l &
show_jobs
myfunc &
# only bash supports wait -n
if test -n "${BASH_VERSION:-}"; then
wait -n
show_jobs
fi
wait
show_jobs
ls | wc -l
show_jobs
}
many_jobs() {
sleep 0.90 &
sleep 0.91 &
sleep 0.92 &
sleep 0.93 &
sleep 0.94 &
sleep 0.95 &
sleep 0.96 &
sleep 0.97 &
sleep 0.98 &
sleep 0.99 &
show_jobs
# Testing this syntax
# Doesn't work because shells say "job %10 not created under job control"
# fg %10
wait
show_jobs
}
osh-debug() {
# lastpipe with 'read'
{ echo x; sleep 0.1; } | sort | read x
jobs --debug
# no lastpipe
{ echo x; sleep 0.1; } | sort | wc -l
jobs --debug
}
"$@"