summaryrefslogtreecommitdiffhomepage
path: root/tests/posix/fdmap.sh
blob: adb632c37a6bbbeb3cdabc296e9d8014d888d742 (plain)
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
# Descriptor plans for spawned commands: every arrangement here is handed
# to spawn()/posix_spawn() as a map rather than performed in the shell.
p() { sh -c "$1"; }
sh -c 'echo out; echo err >&2' >o 2>e; cat o e
sh -c 'echo out; echo err >&2' >oe 2>&1; cat oe
sh -c 'echo out; echo err >&2' 2>&1 >o2 | sed 's/^/piped: /'; cat o2
sh -c 'echo out; echo err >&2' 3>&1 1>&2 2>&3 3>&- 2>/dev/null | sed 's/^/swapped: /'
sh -c 'echo out; echo err >&2' 3>&2 2>&1 1>&3 2>/dev/null
sh -c 'echo to3 >&3' 3>f3; cat f3
sh -c 'echo to9 >&9' 9>f9; cat f9
sh -c 'read x <&4; echo "fd4: $x"' 4<f3
sh -c 'cat <&5' 5<<E
heredoc on fd 5
E
sh -c 'echo x >&3' 3>&- 2>/dev/null || echo "fd 3 closed"
sh -c 'echo x' >&- 2>/dev/null || echo "stdout closed"
sh -c 'read x && echo "read $x"' <&- 2>/dev/null || echo "stdin closed"
sh -c 'echo x >&2' 2>&- ; echo "stderr closed: $(($? != 0))"
exec 6>f6; sh -c 'echo inherited >&6'; exec 6>&-; cat f6
exec 7<f6; sh -c 'cat <&7'; exec 7<&-
exec 8>f8; sh -c 'echo dup >&3' 3>&8; exec 8>&-; cat f8
sh -c 'echo a; echo b >&2' >same 2>same; wc -l <same | tr -d ' '
sh -c 'echo first' >over; sh -c 'echo second' >>over; cat over
sh -c 'cat' <over >copy; cat copy
sh -c 'cat; echo e >&2' <over 2>&1 | sort
echo data | sh -c 'cat >&2' 2>&1 | sed 's/^/via stderr: /'
echo data | sh -c 'cat <&3' 3<&0
sh -c 'echo x' 1>&2 2>/dev/null; echo "order matters: $?"
sh -c 'echo visible' 2>/dev/null 1>&2; echo "(nothing above)"
sh -c 'echo rw >&3; ' 3<>frw; cat frw
i=0; while [ $i -lt 30 ]; do i=$((i+1)); sh -c : 3>f3 4>f9 5<f6; done; echo "no descriptor leak: $(sh -c 'echo ok' 9>&1 >&9)"
V=1 sh -c 'echo "$V"' >fv; cat fv; echo "[${V-unset}]"