Gentoo Archives: gentoo-dev

From: Zac Medico <zmedico@g.o>
To: gentoo-dev@l.g.o
Subject: Re: [gentoo-dev] multiprocessing.eclass: doing parallel work in bash
Date: Sat, 02 Jun 2012 20:40:19
Message-Id: 4FCA79F4.4080204@gentoo.org
In Reply to: Re: [gentoo-dev] multiprocessing.eclass: doing parallel work in bash by Mike Frysinger
1 On 06/02/2012 12:54 PM, Mike Frysinger wrote:
2
3 > # @FUNCTION: redirect_alloc_fd
4 > # @USAGE: <var> <file> [redirection]
5 > # @DESCRIPTION:
6 > # Find a free fd and redirect the specified file via it. Store the new
7 > # fd in the specified variable. Useful for the cases where we don't care
8 > # about the exact fd #.
9 > redirect_alloc_fd() {
10 > local var=$1 file=$2 redir=${3:-"<>"}
11 >
12 > if [[ $(( (BASH_VERSINFO[0] << 8) + BASH_VERSINFO[1] )) -ge $(( (4 << 8) + 1 )) ]] ; then
13 > # Newer bash provides this functionality.
14 > eval "exec {${var}}${redir}'${file}'"
15 > else
16 > # Need to provide the functionality ourselves.
17 > local fd=10
18 > while :; do
19 > if [[ ! -L /dev/fd/${fd} ]] ; then
20 > eval "exec ${fd}${redir}'${file}'" && break
21 > fi
22 > [[ ${fd} -gt 1024 ]] && return 1 # sanity
23 > : $(( ++fd ))
24 > done
25 > : $(( ${var} = fd ))
26 > fi
27 > }
28
29 I launched up a GhostBSD livedvd to see what /dev/fd/ looks like on
30 FreeBSD, and it seems to contain plain character devices instead of
31 symlinks to character devices:
32
33 [ghostbsd@livecd ~]$ uname -a
34 FreeBSD livecd 9.0-RELEASE FreeBSD 9.0-RELEASE #0: Sun Jan 15 17:17:43
35 AST 2012
36 root@××××××××××××××××.org:/usr/obj/i386.i386/usr/src/sys/GHOSTBSD i386
37 [ghostbsd@livecd ~]$ ls -l /dev/fd/
38 total 0
39 crw-rw-rw- 1 root wheel 0, 19 Jun 2 20:15 0
40 crw-rw-rw- 1 root wheel 0, 21 Jun 2 20:15 1
41 crw-rw-rw- 1 root wheel 0, 23 Jun 2 20:15 2
42
43 --
44 Thanks,
45 Zac

Replies