LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-24-2008, 01:28 PM   #1
Bambi535
LQ Newbie
 
Registered: Nov 2007
Posts: 9

Rep: Reputation: 0
Question Read and write to a co-process in bash


Hello all,
Ksh provides the option to read from a co-process by means of:
Code:
#!/bin/ksh
find . -name "*.log" |&
while read -p; do
# whatever
 :
done
How can this be done in bash?
Using a pipeline is no option for me as I set some shell variables inside the while loop that I need to access later on.

Thanks,
Opher.

Last edited by Bambi535; 11-24-2008 at 01:31 PM. Reason: forgot to mark with question icon
 
Old 11-24-2008, 02:41 PM   #2
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
It depends. The evident way is to use a file to hold the data, and read it back from the loop.

You can take the whole list into the loop with <<<, like this
Code:
IFS=' '
n=1
while read line
do
  echo "${line}"
  n=$((n+1))
done <<< $(find .)
echo $n
IFS is the internal field separator marker. <<< will import the whole list into the loop. $n is there so you can check that the var is accessible after while is over.
 
Old 11-24-2008, 06:42 PM   #3
Bambi535
LQ Newbie
 
Registered: Nov 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks for the reply,
Quote:
Originally Posted by i92guboj View Post
It depends. The evident way is to use a file to hold the data, and read it back from the loop.
Indeed, but then I have to clean up temporary files.
Also, I'm not sure how this behaves if I background (&) the first command and in my script read faster than the command generates output ...?
Quote:
You can take the whole list into the loop with <<<, like this
Code:
IFS=' '
n=1
while read line
do
  echo "${line}"
  n=$((n+1))
done <<< $(find .)
echo $n
IFS is the internal field separator marker. <<< will import the whole list into the loop. $n is there so you can check that the var is accessible after while is over.
I'm afraid I was not specific enough. In my script I have several loops and commands reading from the co-process output. So I would need to brace ({ ... }) the bulk of my script. Also the natural order of things is lost
Code:
{
while read; do
# first loop
 :
done
read
...
while read; do
# second loop
 :
done
} < <(find .)
 
Old 11-25-2008, 01:58 PM   #4
Bambi535
LQ Newbie
 
Registered: Nov 2007
Posts: 9

Original Poster
Rep: Reputation: 0
Some more research (google -> bash FAQ) revealed that co-processes are not supported directly.
Instead, one can use a named pipe pair (one for input and one for output).
There are two options: 1) place the extra code in your script.
2) source the functions/coproc.bash (from the 3.2 distribution)
ex.
Code:
  local fifo="/var/tmp/getoptions.$$.$RANDOM"
  mkfifo $fifo || {
    echo "$(basename $0): getoptions: Error creating named pipe. Exiting." >&2
    exit 2
  }
  ( find . >$fifo ; rm -f $fifo ) &
  trap 'echo Received SIGPIPE >&2' SIGPIPE
  globalvar=
  while read; do
  # first loop
    :
  done <$fifo
  # do something with globalvar
  while read; do
  #second loop
    :
  done <$fifo
  trap - SIGPIPE
 
  


Reply

Tags
bash


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash - read or write to specific line in text file? babag Programming 11 08-23-2008 02:44 PM
Should be dead simple - read/write serial port from bash ericcarlson Linux - Software 3 10-21-2006 09:35 PM
read write in bash script yhus Programming 6 09-18-2006 01:23 PM
Bash: read/write the interface? J_Szucs Programming 7 06-04-2004 04:09 PM
Open office read only, K-write read/write mode lwtvh Linux - Newbie 1 07-19-2003 12:33 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration