Results 1 to 5 of 5
Hi to all. That's my first message.
I have a little question to submit I hope that anyone can answer to me:
I must embed a bash instance in a ...
- 07-11-2008 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 2
bash with fifo - eof problem
Hi to all. That's my first message.
I have a little question to submit I hope that anyone can answer to me:
I must embed a bash instance in a software, for submit commands from the software and grab the output.
I thinked to redirect input and output to the bash with two named pipes, using my software for read/write to/from the pipes.
After that I send the first command, the bash read an EOF, executes correctly the command, but then exits.
I must to keep alive the instance, for do another command without re-initiate the bash session.
Does anyone know what I can do for evitate the EOF reading?
- 07-12-2008 #2Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
It's the input redirection that's causing the EOF problem. Would a wrapper script do what you want?
The script you call this wrapper with can then take care of handling the resultant output.Code:#!/bin/bash while read cmd do /bin/bash $cmd done
- 07-13-2008 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 2
[QUOTE=scm;606657]
/bin/bash $cmd
No, I can't, because this way I can a bash instance for every command, and that's what I don't want.
If you call, for example, "cd directory", in the second command the new instance will not be in the right directory.
Thank you for the answer.
- 07-13-2008 #4Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
Ah, I think I now understand your requirement a little better. But I don't have an answer - I'll have to give it some thought.
- 07-14-2008 #5Just Joined!
- Join Date
- Jul 2008
- Posts
- 1
[quote=scm;606657]
This is, in fact, the correct approach.
Simply call "$cmd" within the loop instead of "/bin/bash $cmd" for in-shell execution.
If you need to deal with more complex commands or variables, "eval $cmd" might be an option.
In your software, you have to pay attention that you do not close the named pipe after writing the first command! Therefore, an explicit fopen / fclose should be used.
E.g. bash-like per-line redirection (echo "hello world" >> namedpipe) opens and closes the ouput file/pipe for every operation!


Reply With Quote