3

I am programming something that needs an interface to Bash. At first I thought I could just use popen or QProcess. ( I'm using QT C++ ) They work fine but I can't get them to run Bash in a tty, which you need if you are going to use anything like sudo, which requires a tty/pty to accept a password.

I found some things like forkpty(), openpty(), etc. in the GNU Standard C libraries, but could not figure out how to use them or find any good examples, even after reading their corresponding manpages. Literally, all this part of my program needs to do is be able to read/write from a tty running /bin/bash. Is it really that simple, or is there more to it than meets the eye?

If so, can anyone please provide a concise example on how to do this? Thanks in advance! EDIT: Here's the header file I've come up with!

  • You don't show how you're calling Bash, but have you tried `bash -li`? – Dennis Williamson Jan 28 '10 at 17:16
  • As a matter of fact, I did. That command calls bash as an interactive login shell, but still doesn't run it in a teletype. ;_; I have to write some additional code for it to do so. –  Jan 29 '10 at 17:22
  • possible duplicate of [How do \*nix pseudo-terminals work ? What's the master/slave channel?](http://stackoverflow.com/questions/476354/how-do-nix-pseudo-terminals-work-whats-the-master-slave-channel) – devnull Mar 17 '14 at 11:11

1 Answers1

-2

Consider the option of running /bin/sh -s, which means "read commands from standard input". That removes the need to emulate a terminal.

rohanpm
  • 4,264
  • 17
  • 16
  • Except some commands ( like 'sudo' ) will terminate automatically unless they're run in some sort of tty. ( Emulated or real. ) Just try using sudo in a script. It won't work. You need to make a pty. –  Feb 08 '10 at 00:59