Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6659313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:01:12+00:00 2026-05-26T02:01:12+00:00

Followup to More coproc questions bash coproc and leftover coproc output Given that the

  • 0

Followup to

  • More coproc questions
  • bash coproc and leftover coproc output

Given that the obvious use of coproc does not work as I expected, as seen in:

$ cat test.sh
coproc cat auto/etc/build.cfg
while read -u ${COPROC[0]} BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
do
    echo hello
done

$ bash -x test.sh
+ read -u 63 BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
+ cat auto/etc/build.cfg
+ echo hello
hello
+ read -u BRANCH TARGET SVNSRC SVNTAG BUILDTYPE DISTTYPE DISTARGS
test.sh: line 2: read: BRANCH: invalid file descriptor specification

Question: Why does the coproc go away after the script reads one line of output?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:01:13+00:00Added an answer on May 26, 2026 at 2:01 am

    I cannot reproduce:

    bash-4.1 $ cat infile 
    one
    two
    three
    four
    five
    
    bash-4.1 $ cat s.sh 
    coproc cat infile
    while read -u ${COPROC[0]} v; do
      echo "$v"
    done
    
    bash-4.1 $ bash -x s.sh 
    + read -u 63 v
    + cat infile
    + echo one
    one
    + read -u 63 v
    + echo two
    two
    + read -u 63 v
    + echo three
    three
    + read -u 63 v
    + echo four
    four
    + read -u 63 v
    + echo five
    five
    + read -u 63 v
    + echo ''
    
    + read -u 63 v
    

    Edit: I did reproduce it like this:

    bash-4.1 $ cat s.sh 
    coproc cat infile
    
    sleep 1
    
    while read -u ${COPROC[0]} v; do
      echo "$v"
    done
    
    bash-4.1 $ bash  -x s.sh 
    + sleep 1
    + cat infile
    + read -u v
    s.sh: line 5: read: v: invalid file descriptor specification
    

    Edit: See comments below.


    It seems that the co-process times out quickly …
    May be your system is slow 🙂

    No, the command executed as co-process is too quick,
    if you slow it down, it works:


    bash-4.1 $ cat s.sh 
    coproc while read -r; do
      printf '%s\n' "$REPLY"
      sleep 1
    done < infile
    
    sleep 1
    
    while read -u ${COPROC[0]} v; do
      echo "$v"
    done
    
    bash-4.1 $ bash s.sh 
    one
    two
    three
    four
    five
    

    Anyway, I believe that this test case is not appropriate.
    You need a co-process when you need a two-way pipe (i.e. you
    need to chat with the co-process). You can use a single database
    connection (the database connections are resource expensive)
    and go back and forth with your queries and shell code.

    Edit (see comments below).
    The issues related to the stdin buffering could be worked around
    with some non standard tools (in this case stdbuf is used (part of recent versions of the GNU coreutils, I believe):

    ~/t$ cat s
    coproc stdbuf -oL -i0 mysql
    
    printf '%s;\n' 'show databases' >&${COPROC[1]}
    
    printf '\n\nshowing databases, fisrt time ...\n\n\n'
    
    while read -t3 -u${COPROC[0]}; do
      printf '%s\n' "$REPLY"
      [[ $REPLY == test ]] && {
        printf '%s\n' 'test found, dropping it ...'
        printf '%s;\n' 'drop database test' >&${COPROC[1]}
        }
    done
    
    printf '\n\nshowing databases, second time ...\n\n\n'
    
    
    printf '%s;\n' 'show databases' >&${COPROC[1]}
    
    while read -t3 -u${COPROC[0]}; do
      printf '%s\n' "$REPLY"
    done
    
    
    printf '%s\n' quit >&${COPROC[1]}
    

    Output:

    ~/t$ bash s
    
    
    showing databases, fisrt time ...
    
    
    Database
    information_schema
    mysql
    sakila
    test
    test found, dropping it ...
    world
    
    
    showing databases, second time ...
    
    
    Database
    information_schema
    mysql
    sakila
    world
    

    I realize this approach has many drawbacks …

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a follow up to Krzysztof’s statement that Windsor does a lot more than
Followup to the following Questions: What lib in the gecko 1.9.3 SDK do I
NOTE: This is a followup to my question here. I have a program that
A followup to another question I did, I've done some more digging but I
We have a mid-size SQL Server based application that has no indexes defined. Not
More specifically, are there any databases that don't require secondary storage (e.g. HDD) to
This is more of a follow-up to questions 1 & 2 . As told
This is a more specific question to follow up on [another question that I
This is a followup question to PHP: if more than ..px resize to As
Followup on an answer from last night - I was hoping more comments would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.