Find the answer to your Linux question:
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 ...
  1. #1
    Just 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?

  2. #2
    scm
    scm is offline
    Linux 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?
    Code:
    #!/bin/bash
    while read cmd 
    do
        /bin/bash $cmd
    done
    The script you call this wrapper with can then take care of handling the resultant output.

  3. #3
    Just 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.

  4. #4
    scm
    scm is offline
    Linux 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.

  5. #5
    Just 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!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...