开发者

Execute commands inside another process?

I want to be able to start a process and s开发者_JS百科end input to it immediately.

Take Bash as an example.

The following code will enter another Bash process and then print "Hello World!" on the screen after I have terminated the process with "exit"

bash
echo "Hello World!"

Is there a way to enter bash and then print "Hello World!" INSIDE that process?

I'm using Ruby and Bash on Ubuntu.

UPDATE: This question was not intended to be Bash specific. Bash was just an example. It would be better if someone could post an answer that handles all other binaries.


You may be looking for the expect tool.


bash -c 'echo "Hello World!"'

You can also try writing bash script and invoking it:

bash ./myscript

or put #!/bin/bash as the first line in a text file and it will be invoked using bash like any other executable:

./myscript

Update0

Bash is an interpreter. There are many other interpreters, I'd highly recommend you take a look at Python, you can send instructions to be interpreted to these programs easily enough.

You might also be referring to the Unix IO-model, in which case you may want to ask a question relating to the use of piping with stdin and stdout.


%x(some external bash commands)

`ls -1`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜