Project

General

Profile

« Previous | Next » 

Revision 9680

lib/sh/util.sh: to_file(): added del= flag to prevent the file from being auto-removed on error (e.g. to preserve a partial result, which normally would be removed)

View differences:

lib/sh/util.sh
656 656
require_not_exists() # usage: require_not_exists file || return 0
657 657
{ test ! -e "$1" || what="file \"$1\"" already_exists_msg; }
658 658

  
659
function to_file() # usage: stdout=... [if_not_exists=1] to_file cmd...
659
function to_file() # usage: stdout=... [if_not_exists=1] [del=] to_file cmd...
660 660
# auto-removes a command's output file on error (like make's .DELETE_ON_ERROR)
661 661
{
662 662
	echo_func; kw_params stdout
663
	: "${stdout?}"
663
	: "${stdout?}"; local del="${del-1}"
664 664
	if test "$if_not_exists"; then require_not_exists "$stdout" || return 0; fi
665 665
	
666 666
	local redirs=("${redirs[@]}" ">$stdout")
667
	"$@" || { save_e; unset redirs; rm "$stdout"; rethrow; }
667
	"$@" || { save_e; unset redirs; test ! "$del" || rm "$stdout"; rethrow; }
668 668
}
669 669
alias to_file='"to_file" ' # last space alias-expands next word
670 670

  

Also available in: Unified diff