Project

General

Profile

« Previous | Next » 

Revision 9685

lib/sh/util.sh: added redir() and use it in command() to perform and echo the redirections

View differences:

lib/sh/util.sh
584 584

  
585 585
disable_logging() { set_fds "$log_fd>/dev/null"; }
586 586

  
587
function command()
588
# usage: [cmd_log_fd=|1|2|#] [redirs=...] [verbosity_min=] command extern_cmd...
587
function redir() # usage: local redirs=(#<>...); redir cmd...; unset redirs
589 588
# to view only explicitly-displayed errors: explicit_errors_only=1 script...
590 589
{
590
	log++ echo_func; kw_params redirs
591
	(
592
		log++ set_fds "${redirs[@]}"
593
		(case "$1" in command__exec) shift;; esac; echo_redirs_cmd)
594
		"$@"
595
	) || return
596
}
597
alias redir='"redir" ' # last space alias-expands next word
598

  
599
command() # usage: [cmd_log_fd=|1|2|#] [verbosity_min=] command extern_cmd...
600
{
591 601
	log++ echo_func; kw_params cmd_log_fd redirs verbosity_min
592 602
	# if no cmd_log_fd, limit log_fd in case command uses util.sh
593 603
	local cmd_log_fd="${cmd_log_fd-$log_fd}"
594 604
	local redirs=("${redirs[@]}")
595 605
	
596
	# determine redirections now so they can be logged along with the command
606
	# determine redirections
597 607
	if test "$cmd_log_fd"; then
598 608
		if can_log; then
599 609
			if test "$cmd_log_fd" != "$log_fd"; then
......
603 613
		fi
604 614
	fi
605 615
	
606
	cmd2rel_path; echo_redirs_cmd
607
	
608
	(
609
		if can_log; then indent; fi
610
		log++ set_fds "${redirs[@]}"
611
		
612
		if test "$verbosity_min"; then verbosity_min "$verbosity_min"; fi
613
		
614
		exec -- "$@" # -- so cmd name not treated as `exec` option
615
	) || die_e
616
	cmd2rel_path
617
	redir command__exec "$@" || die_e
616 618
}
619
command__exec()
620
{
621
	ensure_nested_func
622
	if can_log; then indent; fi
623
	if test "$verbosity_min"; then verbosity_min "$verbosity_min"; fi
624
	exec -- "$@" # -- so cmd name not treated as `exec` option
625
}
617 626

  
618 627
# auto-echo common external commands
619 628
for cmd in env rm; do alias "$cmd=command $cmd"; done; unset cmd

Also available in: Unified diff