Path: csiph.com!xmission!news.glorb.com!usenet.stanford.edu!not-for-mail From: konsolebox Newsgroups: gnu.bash.bug Subject: Re: redirecting a file descriptor to an array variable? Possible? How? RFE? Date: Mon, 16 Nov 2015 19:51:23 +0800 Lines: 97 Approved: bug-bash@gnu.org Message-ID: References: <564532BD.60801@tlinx.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: usenet.stanford.edu 1447704957 31100 208.118.235.17 (16 Nov 2015 20:15:57 GMT) X-Complaints-To: action@cs.stanford.edu Cc: bug-bash To: Linda Walsh Envelope-to: bug-bash@gnu.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=vu+pgqY51k5nvXBf9R6n2OVC8odp/0fPlfcOVXr6pIo=; b=K42PUC7xMxfouu39d8N0ZnjJ7PmQ1Zo9O+kHHCvi35P4GFz1vCdFI1YxUhpqkjyOdb Yuub/1pzFjwVUqxZKhi1vKjRbWdfSHNtNkwHHazuFrr3UkpgaAKWtcyoo2aO/6mb0hB5 ZC9IHHxMIHGmj4/T+Ae94jsReVWEgbZOO42njDc8RnB+KfR19R9sh8EuAWUaiduJekYl CfiqHstc/0cug4Qz98O2TsGl6HbUxJ84VG/WF2Z8nsqDzkdN9Ezmc19parY6Ojg2/n2j KYqCTWKTcV62V69yKLag3NGNa/Ahf6Mv7UnYueGWDF6sDq7MnDy2OTll4qpRb11Wyc64 eiwQ== X-Received: by 10.28.145.134 with SMTP id t128mr17385999wmd.64.1447674683110; Mon, 16 Nov 2015 03:51:23 -0800 (PST) In-Reply-To: <564532BD.60801@tlinx.org> X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:400c:c09::22f X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11887 On Fri, Nov 13, 2015 at 8:45 AM, Linda Walsh wrote: > > I'd like to be able to record stdout and stderr > without using any temp files into bash array files, AND > record the status of the command executed. > You can use coproc processes that would act as buffers. Obvious Note: Avoid it if you're conservative about some undocumented areas that would look too hacky to you. Concept: #!/bin/bash function sponge { while read -r __ && [[ $__ != __EOF__ ]]; do ## Or use a keyword that's more unusual. Closing one end of the input pipe seems unreliable. I also tried using traps for flushing output; didn't work so well. (But you can try those ideas. I haven't tested how adding `sleep` affects them.) LINES+=("$__") done printf '%s\n' "${LINES[@]}" exec sleep 1 } function run_with_captured_output { local C0 C1 P0 P1 R __ exec 4>&2 2>/dev/null ## Avoid warning. coproc C0 (sponge) P0=$! coproc C1 (sponge) P1=$! exec 2>&- 2>&4 4>&- disown "$P0" "$P1" ## This may be needed or not. "$@" >&"${C0[1]}" 2>&"${C1[1]}" R=$? __A0=() __A1=() echo __EOF__ >&"${C0[1]}" while read -u "${C0[0]}" -t 0.1 __; do __A0+=("$__") done echo __EOF__ >&"${C1[1]}" while read -u "${C1[0]}" -t 0.1 __; do __A1+=("$__") done # TODO: Not sure how a proper coproc cleanup is done. Closing FDs does funny things. Perhaps it's automatic, but not sure how disown affects it. We could examine the source code but not sure if that would be reliable enough for all versions of bash including the upcoming ones. return "$R" } function create_random_output { local I for (( I = 0; I < 5; ++I )); do echo "OUT: $RANDOM" echo "ERR: $RANDOM" >&2 done } run_with_captured_output create_random_output echo "Returned status: $?" printf 'Stdout: %s\n' "${__A0[@]}" printf 'Stderr: %s\n' "${__A1[@]}" Output: Returned status: 0 Stdout: OUT: 26079 Stdout: OUT: 21138 Stdout: OUT: 17971 Stdout: OUT: 8460 Stdout: OUT: 7185 Stderr: ERR: 19874 Stderr: ERR: 4559 Stderr: ERR: 18830 Stderr: ERR: 19818 Stderr: ERR: 21562