LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-31-2006, 10:46 AM   #1
greno
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?
----------------------------
#!/bin/bash
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, 03:19 PM   #2
ta0kira
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.
ta0kira
 
Old 12-31-2006, 03:23 PM   #3
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709

Rep: Reputation: 428Reputation: 428Reputation: 428Reputation: 428Reputation: 428
Hello!

Command
Code:
exec 8< /tmp/fifo
itself hangs up because FIFO must be opened for reading and writing simultaneously. You can use
Code:
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:
Code:
#!/bin/bash
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:
Code:
#!/bin/bash
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:
Code:
#!/bin/bash
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 03:44 PM.
 
1 members found this post helpful.
  


Reply



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 03:45 AM
Redirect /dev/ttyS0 input/output to tty2 Kymbo Linux - General 1 09-07-2006 10: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 03:56 AM
NFS Input/Output error after filesystem mounted problem wahhh Linux - Networking 1 09-06-2004 07:48 PM
bash: cd: /mnt/floppy/: Input/output error embsupafly Linux - Hardware 1 08-16-2003 06:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:01 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