help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

possible to use a function that uses a coproc in a pipeline?


From: Britton Kerin
Subject: possible to use a function that uses a coproc in a pipeline?
Date: Fri, 31 Mar 2023 13:47:35 -0800

Overview:

I've been playing around more with coproc and I ran into an issue
trying to use a function that uses a server coproc in a pipeline.  It
doesn't work and I can't see how to make it work without
pipe ends available in subshells.  I wonder if I'm missing some
obvious way or if it might be possible to add an option to coproc to
not close the pipe ends?

Details:

The coproc in this case is a sqlite3 REPL client, the setup looks
about like this:

     function startSqliteServerCoproc {
       coproc sqliteServer { sqlite3 -interactive $@; }
       [snip some details]
     }

     function sendToSqliteServer {
       # Send given command plus a request for marker afterwords, roughly:
       {
         echo "$given_commands";
         echo "$marker_print";
       } >&"${sqliteServer[1]}";
     }

     function readFromSqliteServer {
       # Read until mark
       [snip]
     }

     function sl {
       # This is the function to use to enter queries on the command line
       sendToSqliteServer "$@" && readFromSqliteServer
     }

This works nicely:

     $ startSqliteServerCoproc /tmp/test.db
     [1] 262949
     SQLite version 3.31.1 2020-01-27 19:55:54
     Enter ".help" for usage hints.
     $ sl SELECT \* from test_table
     test_col
     ----------
     foo
     $

But this doesn't:

     $ sl SELECT \* from test_table | grep foo
     bash: "${sqliteServer[1]}": Bad file descriptor

It looks like the STDOUT redirect in the send command collides with
the one in the pipeline, or maybe the sl is getting am implicit
process of it's own.  It seems like the readFromSqliteServer needs to
be in a subshell, but of course the pipe file descriptors isn't
available then:

     $ (sl SELECT \* from test_table)
     bash: "${sqliteServer[1]}": Bad file descriptor

Britton



reply via email to

[Prev in Thread] Current Thread [Next in Thread]