OILS / demo / fd-number.sh View on Github | oils.pub

31 lines, 9 significant
1#!/bin/sh
2#
3# Usage:
4# demo/fd-number.sh 8 # use FD 8
5# demo/fd-number.sh 100 # use FD 100
6# demo/fd-number.sh 1024 # use FD 1024
7# demo/fd-number.sh 1024 T # use FD 1024, but RAISE default ulimit to allow it
8#
9# Derived from Andriy's example:
10#
11# exec 25>out
12# echo hello>&25
13# cat out
14#
15# Except we generalize the FD number with 'eval'
16
17set -o errexit
18
19fd=${1:-8}
20set_ulimit=${2:-}
21
22# raise limit if necessary
23if test -n "$set_ulimit"; then
24 ulimit -n "$(( fd + 2 ))" # +2 needed, not just +1 ?
25fi
26
27eval "exec $fd> _tmp/hello"
28echo "using fd $fd" >& $fd
29
30# with OSH, defeat 'builtin cat' optimization
31$(which cat) _tmp/hello