Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you connect the keyboard and a named pipe to STDIN in Bash?
    text
    copied!<p>I'm trying to find a way to invoke an interactive command-line program so that it takes input both directly from the keyboard and also from a named pipe. My goal is to get this working with MATLAB, but I think Bash works just as well as an example. So the idea is to start Bash and once it's running I can type commands, use up arrow for history, etc, and also send commands to a named pipe. I've been looking around and fiddling with this for a few days, but nothing I've tried so far has worked quite right.</p> <p>For example, there's a helpful thread at <a href="https://serverfault.com/questions/171095/how-do-i-join-two-named-pipes-into-single-input-stream-in-linux">https://serverfault.com/questions/171095/how-do-i-join-two-named-pipes-into-single-input-stream-in-linux</a> that suggests doing something like this:</p> <pre><code>mkfifo alt_in (tail -f alt_in &amp; cat) | bash </code></pre> <p>This is almost exactly what I'm looking for, except that if you try to use backspace or arrow keys it doesn't work right. (I guess this is because <code>cat</code> is intercepting the keystrokes, which would normally be handled by the readline library in bash?) Does anyone have other suggestions?</p> <p>Ultimately I'd like to have a way to launch a MATLAB process so that I can send commands to it from TextMate but also interact with it in the terminal. I use MATLAB for work, but am no great fan of its GUI or editor.</p> <p><strong>Edit December 19, 2010</strong></p> <p>Thanks for all the very helpful suggestions! I wanted to summarize the outcome of this for anyone who's interested. I looked into <code>rlwrap</code>, but since my goal was to get this working inside a TextMate bundle that anyone could use, I was hesitant to rely on a non-standard utility (which I should have mentioned in the first place). I also checked on <code>expect</code> and <code>unbuffer</code> briefly, but, well, they seem pretty complicated and I just didn't have the fortitude to dive in to that.</p> <p><code>screen</code> worked pretty well for this - as suggested below, I could open Terminal, start <code>screen</code>, start <code>matlab -nodesktop</code>, and then it was possible to have TextMate, for example, send the selected text to Matlab using <code>screen -X ...</code> inside a TextMate command. The shortcomings of this approach that I noticed were:</p> <ol><li>Sending a block of text with more that 1,024 characters resulted in an error from `screen`; I figure there's some buffer somewhere that can't hold more than that, but didn't spend any time trying to track it down.</li> <li>Screen has its own scrollback buffer, making Terminal's scrollbar pretty useless. Maybe there's an option to change how this works?</li> <li>Control-A is `screen`'s command keystroke; you can't use it for moving to the beginning of the line unless you rebind the command key.</li></ol> <p>While I was playing around with <code>screen</code>, it occurred to me that although the question I asked is about connecting pipes and STDIN and so on, I actually only care about this for a very specific case: getting text from TextMate to Terminal. And that prompted me to try doing this with AppleScript, and surprisingly that turned out to be simpler and more robust than any of the other things I'd tried. Create a TextMate command that takes the selected text or current line as input, and contains</p> <pre><code>#!/usr/bin/env osascript set input_command to do shell script "cat" tell application "Terminal" do script input_command in window 1 end tell </code></pre> <p>And that works great, as long as MATLAB is in the frontmost Terminal window. (It's also possible to search for a Terminal tab that's running MATLAB, I just excluded that for clarity.)</p> <p>The moral of this story is that I should ask more specific questions. Thanks again for all the help; I've learned a lot from wrestling with this!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload