Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #16513

Re: Undocumented feature: Unnamed fifo '<(:)'

From felix <felix@f-hauri.ch>
Newsgroups gnu.bash.bug
Subject Re: Undocumented feature: Unnamed fifo '<(:)'
Date 2020-07-01 19:21 +0200
Message-ID <mailman.777.1593624078.2574.bug-bash@gnu.org> (permalink)
References <20200628134945.GB24863@medium.hauri> <CAPSX3sTo8-kUffZ8NjkPXjiubshGYaP_Sw=nNro+tnEJ-kCD+A@mail.gmail.com> <20200701172104.GE2143@medium.hauri>

Show all headers | View raw


Thanks Pierre,

On Sun, Jun 28, 2020 at 10:48:42PM +0200, Pierre Gaston wrote:
> Maybe "coproc" is already the feature you need (limited to only 1 though)?

Correct: I missed this!

This work fine:

  coproc stdbuf -o0 date -f -  +%s 2>&1
  DATEIN=${COPROC[1]} DATEOUT=$COPROC
  echo >&$DATEIN 2009-02-13 23:31:30 UTC;read -u $DATEOUT out;declare -p out
  declare -- out="1234567890"
  echo foo bar >&$DATEIN ;read -u $DATEOUT out;declare -p out
  declare -- out="date: invalid date 'foo bar'"

Unfortunely, coproc don't have option for separated error output...
If I want to handle errors, I could do this by using ``<(:)'' again:

  exec 8<> <(:)
  coproc stdbuf -o0 date -f -  +%s 2>&8
  DATEIN=${COPROC[1]} DATEOUT=$COPROC DATEERR=8
  bound=wrong_date_$(uuidgen)
  date_to_epoch() { 
      local _out
      echo ${@:2}$'\n'$bound 1>&$DATEIN
      read -u $DATEERR _out
      if [ -z "${_out//*$bound*}" ]; then
          read -u $DATEOUT $1
      else
          printf -v $1 %s "$_out"
          read -u $DATEERR _out
          return -1
      fi
  }

  Usage: date_to_epoch <variable name> <unquoted date to convert>

Then

  if date_to_epoch result 2009-02-13 23:31:30 UTC ;then
	echo $result ; else echo ERROR: $result ;fi
  1234567890

  if date_to_epoch result bad entry  ;then
	echo $result ; else echo ERROR: $result ;fi
  ERROR: date: invalid date 'bad entry'

Again, I use this for not only with `date` and `bc`, but with `mysql`, `ftp`
 or even `ssh` too.

-- 
 Félix Hauri  -  <felix@f-hauri.ch>  -  http://www.f-hauri.ch

Back to gnu.bash.bug | Previous | Next | Find similar


Thread

Re: Undocumented feature: Unnamed fifo '<(:)' felix <felix@f-hauri.ch> - 2020-07-01 19:21 +0200

csiph-web