OILS / demo / open-fds.sh View on Github | oilshell.org

54 lines, 26 significant
1#!/bin/sh
2
3# bash: descriptors 10 and 255
4# osh: 3 and 4 taken: BAD
5
6# dash: 10 and 11
7# mksh: 24 25 26
8# zsh: 10 11 12. zsh somehow doesn't run this script correctly. It's not
9# POSIX I guess.
10
11# count FDs greater than 10. 0-9 are reserved for scripts.
12count_func() {
13 local count=0
14 local reserved=0
15
16 local pid=$$
17
18 # Uncomment this to show the FD table of the pipeline process! The parent
19 # doesn't change!
20
21 #local pid=$BASHPID
22
23 for path in /proc/$pid/fd/*; do
24 echo $path
25 count=$((count + 1))
26 local fd=$(basename $path)
27 if test $fd -gt 2 && test $fd -lt 10; then
28 reserved=$((reserved + 1))
29 fi
30 done
31
32 ls -l /proc/$pid/fd
33
34 echo "$count FDs open; $reserved are RESERVED (3-9)"
35}
36
37# What does it look like inside a redirect? _tmp/err.txt must be open.
38count_redir() {
39 {
40 count_func
41 } 2> _tmp/err.txt
42}
43
44count_pipeline() {
45 count_func | cat
46}
47
48# https://stackoverflow.com/questions/2493642/how-does-a-linux-unix-bash-script-know-its-own-pid
49pid() {
50 echo $$ $BASHPID | cat
51}
52
53"$@"
54