From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-1.1 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from primenet.com.au (ns1.primenet.com.au [203.24.36.2]) by inbox.vuxu.org (OpenSMTPD) with ESMTP id 091adbc7 for ; Fri, 16 Aug 2019 10:05:54 +0000 (UTC) Received: (qmail 9028 invoked by alias); 16 Aug 2019 10:05:42 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: List-Unsubscribe: X-Seq: 24149 Received: (qmail 18545 invoked by uid 1010); 16 Aug 2019 10:05:42 -0000 X-Qmail-Scanner-Diagnostics: from mail-wr1-f53.google.com by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.101.2/25538. spamassassin: 3.4.2. Clear:RC:0(209.85.221.53):SA:0(-2.0/5.0):. Processed in 4.407302 secs); 16 Aug 2019 10:05:42 -0000 X-Envelope-From: stephane.chazelas@gmail.com X-Qmail-Scanner-Mime-Attachments: | X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _netblocks.google.com designates 209.85.221.53 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mail-followup-to:references :mime-version:content-disposition:in-reply-to:user-agent; bh=yW7X4PiodxCJs34mqXmGQRTU3qOeMMUo9yKIgEhirQ8=; b=a5+h+Ec7AkSh6NTL0vl/RcTOjygELiMHLeuzcxWzP+r0sq9LWP6nGme+mYRI6lOXAf POJoIlCb9MZMXpclaANdgk7HafcamXcQUPEM2y4+/3ggA3oBpUnfFS7+6Zz/BopAxbpz XmJJnot8HyLzaEeXHJPCIPedngyZO2gkxHHNb4HQsRQBcq5E8RIv5gt1GoAAiw+M2sG+ Sl+XYukQxTFocKasDFDP5rMNXgHex0CZ1p/uDp0Pd8HsaWTxBHbGQVnMFj0Za0hUm+Gw QNWB7LaFKCcF1OeIQraItofrM4UmjsBazjWpGAIahG4n1d0h5allHfCoG85UhZiyT+xF z9Tg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id :mail-followup-to:references:mime-version:content-disposition :in-reply-to:user-agent; bh=yW7X4PiodxCJs34mqXmGQRTU3qOeMMUo9yKIgEhirQ8=; b=VUzWnKikc+LYn8tdgKc27sKs2vGc0qrlqMHl+NhAam1M8jTZgG5rjC9TaOjl4rLqsq 57fG6GhCHPdcDYiHnT/JYJNMYpwy5ciAyq+VgkLy1uNJoSmt247qIh9ubvWt1Pm7qpMv Zr5Vd9LY3jesicSX6B3Dw0c45E25n8N+Jdv3Psvh48jmInX4tbVg30r5umzwx2TjGxxO XIk6toldvZBzNQaWDqdFiE0AT4ao7OQmne3M83vvEZt3h2TGTOjJsezn66e+nsJhQM1z r2PVKJcM8Lsdan7tfjsdXxri1fUXqgfOS3QrC1jV11V5q27SBqKoX+Ek3Z5iAK5+17/P grIA== X-Gm-Message-State: APjAAAWaRuQvNfPlX6exw5/jrDCOt1n7PHyOZ3CbMNHPH/MvYu6svc1U /FQAEhRV3eBxUU3V7HcXdLA= X-Google-Smtp-Source: APXvYqwrWgly5g9vYgzVX1ij9oKnHFPRMAywJJ4MMauscq+72H890gSvp0xSgtqUQRSrT4rrBgYIJg== X-Received: by 2002:adf:9482:: with SMTP id 2mr9699865wrr.91.1565949903989; Fri, 16 Aug 2019 03:05:03 -0700 (PDT) Date: Fri, 16 Aug 2019 11:05:01 +0100 From: Stephane Chazelas To: Aryn Starr Cc: zsh-users@zsh.org Subject: Re: Capture stdout, stdin, and exit status in different variables without using temporary files Message-ID: <20190816100501.nsvfbdwwnoaihlhe@chaz.gmail.com> Mail-Followup-To: Aryn Starr , zsh-users@zsh.org References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20171215 I assume you meant stderr and not stdin which would make little sense. And the bash wiki pages refers to capturing both stdout and err, not stdin (whatever that means). 2019-08-15 20:22:38 +0430, Aryn Starr: > If not, is a named pipe advantageous to a temporary file? With named pipes, you'd get deadlocks unless you use a select()/poll() loop like for unnamed pipes. > Is there a way to avoid disk IO (which will probably slow things down considerably)? To expand on the solution in the link I gave earlier to also capture the exit status, that would be: << #! /bin/zsh - zmodload zsh/zselect zmodload zsh/system (){exec {wo}>$1 {ro}<$1} <(:) # like yash's wo>>|ro (but on Linux only) (){exec {we}>$1 {re}<$1} <(:) # the command (here ls as an example) ls -d / /x >&$wo 2>&$we & pid=$! exec {wo}>&- {we}>&- out= err= o_done=0 e_done=0 while ((! (o_done && e_done))) && zselect -A ready $ro $re; do if ((${#ready[$ro]})); then sysread -i $ro && out+=$REPLY || o_done=1 fi if ((${#ready[$re]})); then sysread -i $re && err+=$REPLY || e_done=1 fi done wait "$pid"; exit_status=$? printf '%s: %s\n' stdout "$out" stderr "$err" 'exit status' "$exit_status" >> Which gives: stdout: / stderr: ls: cannot access '/x': No such file or directory exit status: 2 (note that in $out and $err, the trailing newline character is not removed). -- Stephane