Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-31-2006, 11:46 AM   #1
LQ Newbie
Registered: May 2004
Posts: 13

Rep: Reputation: 0
bash input/output redirect problem

Can anyone tell me why the following sometimes works from the command line but refuses to work at all (it hangs) in a shell script file? Is there something buggy about fifo's?
rm /tmp/fifo
mkfifo /tmp/fifo # create fifo
exec 8< /tmp/fifo # open fd 8 for reading
exec 9> /tmp/fifo # open fd 9 for writing
echo fifocontent >&9 & # dump some content to fifo - don't block script
cat <&8 # read content from fifo

bash: 3.1.17(1) on FC6
bash: 2.05b.0(1) on RH9
Old 12-31-2006, 04:19 PM   #2
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Try putting the exec lines in the background with &. I can't imagine it working on the command line or in the script without the first being in the background.
Old 12-31-2006, 04:23 PM   #3
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 705

Rep: Reputation: 425Reputation: 425Reputation: 425Reputation: 425Reputation: 425

exec 8< /tmp/fifo
itself hangs up because FIFO must be opened for reading and writing simultaneously. You can use
exec  8<>/tmp/fifo
instead, but in this case one can not close only one end of FIFO because both ends are the same. Here is an example:
rm /tmp/fifo
mkfifo /tmp/fifo 
exec 8<> /tmp/fifo
echo fifocontent >&8 &
cat <&8
This script do the work, but hangs up on last command (`cat') because write-end of FIFO (fd 8) not closed (as well as read-end fd=8).

P.S.: putting exec in background can't help because new file descriptor is accessible in subshell only (I think). Following script exhibits this behaviour:
exec 3>&1 &
echo "test-message"  >&3 # fd 3 is unaccessible if
                         # we  use subshell (&) in
                         # previous line
This script will work if we delete &-sign at the tail of second line.
One can use following approach, though:
rm /tmp/fifo
mkfifo /tmp/fifo 
(exec 9> /tmp/fifo; echo fifocontent >&9)&
(exec 8< /tmp/fifo; cat <&8) &
exit  0;

Last edited by firstfire; 12-31-2006 at 04:44 PM.
1 members found this post helpful.


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: How to Redirect Output to File, AND Still Have it on Screen edwin11 Linux - Software 27 10-20-2017 04:45 AM
Redirect /dev/ttyS0 input/output to tty2 Kymbo Linux - General 1 09-07-2006 11:07 AM
Is there a way in Linux/Unix bash to turn off output or redirect to a file jimwelc Linux - Newbie 6 01-06-2005 04:56 AM
NFS Input/Output error after filesystem mounted problem wahhh Linux - Networking 1 09-06-2004 08:48 PM
bash: cd: /mnt/floppy/: Input/output error embsupafly Linux - Hardware 1 08-16-2003 07:36 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:06 PM.

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