Revision 9254
Added by Aaron Marcuse-Kubitza almost 12 years ago
util.sh | ||
---|---|---|
394 | 394 |
require_fd_not_exists() # usage: require_fd_not_exists fd || return 0 |
395 | 395 |
{ ! fd_exists "$1" || type=info die "fd $1 already exists, skipping"; } |
396 | 396 |
|
397 |
set_fd() # usage: dest=fd dir='[<>]' src=fd [noclobber=1] set_fd
|
|
397 |
set_fd() # usage: dest=fd expr='[<>]&fd' [noclobber=1] set_fd
|
|
398 | 398 |
{ |
399 | 399 |
echo_func; kw_params dest dir src |
400 |
: "${dest:?}" "${dir:?}" "${src:?}"
|
|
400 |
: "${dest:?}" "${expr:?}"
|
|
401 | 401 |
test ! "$noclobber" || require_fd_not_exists "$dest" || return 0 |
402 |
echo_eval exec "$dest$dir&$src"
|
|
402 |
echo_eval exec "$dest$expr"
|
|
403 | 403 |
} |
404 | 404 |
|
405 |
shadow_fd() { noclobber=1 set_fd; } # usage: dest=fd src=fd dir='[<>]' shadow_fd
|
|
405 |
shadow_fd() { noclobber=1 set_fd; } # usage: dest=fd expr='[<>]&fd' shadow_fd
|
|
406 | 406 |
|
407 | 407 |
# convention: use fd 10/11/12 for command-specific alternate stdin/stdout/stderr |
408 | 408 |
|
... | ... | |
412 | 412 |
{ |
413 | 413 |
log++; echo_func; log++ |
414 | 414 |
# ignore errors if a source fd isn't open |
415 |
dest=20 src=0 dir='<' shadow_fd || true
|
|
416 |
dest=21 src=1 dir='>' shadow_fd || true
|
|
417 |
dest=22 src=2 dir='>' shadow_fd || true
|
|
415 |
dest=20 expr='<&0' shadow_fd || true
|
|
416 |
dest=21 expr='>&1' shadow_fd || true
|
|
417 |
dest=22 expr='>&2' shadow_fd || true
|
|
418 | 418 |
} |
419 | 419 |
set_global_fds |
420 | 420 |
err_fd=22 # global stderr |
Also available in: Unified diff
lib/sh/util.sh: set_fd()/shadow_fd(): take just a redirect source expr containing the <> and the fd, to allow the caller to specify these in the shell redirection syntax. this also allows using a file (such as /dev/null) as the redirect source.