标签: exec

使用System.setOut()重定向Runtime.getRuntime().exec();

我有一个程序Test.java:

import java.io.*;

public class Test {
    public static void main(String[] args) throws Exception {
        System.setOut(new PrintStream(new FileOutputStream("test.txt")));
        System.out.println("HelloWorld1");
        Runtime.getRuntime().exec("echo HelloWorld2");
    }
}
Run Code Online (Sandbox Code Playgroud)

这应该将HelloWorld1和HelloWorld2打印到文件text.txt.但是,当我查看文件时,我只看到HelloWorld1.

  1. HelloWorld2去了哪里?它消失在空气中吗?

  2. 假设我想将HelloWorld2重定向到test.txt.我不能在命令中添加">> test.txt",因为我将得到一个文件已经打开错误.那我该怎么做?

java redirect runtime exec runtime.exec

23
推荐指数
2
解决办法
4万
查看次数

从Common Lisp执行shell命令

如何在Common Lisp程序中执行shell(bash)命令并将输出分配给变量?

lisp bash shell common-lisp exec

23
推荐指数
6
解决办法
1万
查看次数

如何正确使用fork,exec,等待

我正在写的shell需要执行用户给它的程序.这是我的程序的缩短版本

int main()
{
    pid_t pid = getpid(); // this is the parents pid

    char *user_input = NULL;
    size_t line_sz = 0;
    ssize_t  line_ct = 0; 

    line_ct = getline(&user_input, &line_sz, stdin); //so get user input, store in user_input

    for (;;)
    {
        pid_t child_pid = fork(); //fork a duplicate process

        pid_t child_ppid = getppid(); //get the child's parent pid

        if (child_ppid == pid) //if the current process is a child of the main process
        {
            exec(); //here I need to execute …
Run Code Online (Sandbox Code Playgroud)

c shell exec

23
推荐指数
1
解决办法
8万
查看次数

在getRuntime().exec中使用引号

我想使用字符串作为输入来调用bash.就像是:

sh -l -c "./foo"
Run Code Online (Sandbox Code Playgroud)

我想从Java做到这一点.不幸的是,当我尝试使用命令调用时getRuntime().exec,我收到以下错误:

      foo": -c: line 0: unexpected EOF while looking for matching `"'

      foo": -c: line 1: syntax error: unexpected end of file
Run Code Online (Sandbox Code Playgroud)

它似乎与我的字符串没有被EOF终止有关.

有没有办法将特定于平台的EOF插入Java字符串?或者我应该寻找另一种方法,比如在调用"sh"之前写入临时脚本?

java exec runtime.exec

22
推荐指数
2
解决办法
2万
查看次数

如何使用变量在bash中指示文件描述符?

我想使用bash变量来指示文件描述符,如下所示:

id=6
file=a
exec $id<>$file
Run Code Online (Sandbox Code Playgroud)

但用法是错误的:

-bash: exec: 6: not found
Run Code Online (Sandbox Code Playgroud)

那么,如何使用变量来指示exec命令中的文件描述符?

io bash file-descriptor exec

22
推荐指数
2
解决办法
6790
查看次数

在C中,如何在进行execvp()或类似调用时将stdin/stdout/stderr重定向到文件?

我有以下代码:

pid_t pid = fork();
if (pid == -1)
{
    // ...
}
else if (pid == 0)
{
    stdin = someopenfile;
    stdout = someotherfile;
    stderr = somethirdopenfile;
    execvp(args[0], args);
    // handle error ...
}
else
{
    // ...
}
Run Code Online (Sandbox Code Playgroud)

问题是,execvp()调用的输入/输出仍然是控制台,而不是文件.显然,我做错了什么,做这件事的正确方法是什么?

c unix linux exec io-redirection

22
推荐指数
2
解决办法
8万
查看次数

spawn和exec有什么区别?

我正在学习编写TCL(期望)脚本,我注意到一些示例显示使用spawn,而其他示例显示命令exec.我试过谷歌搜索,但找不到有什么区别?

假设我在一个长期预期脚本的中间调用'exec',我可以期待发生什么?

scripting tcl exec expect spawn

21
推荐指数
3
解决办法
2万
查看次数

无法通过exec()语句更改函数中的全局变量?

为什么我不能使用exec()从函数内部更改全局变量?当赋值语句在exec()之外时,它工作正常.这是我的问题的一个例子:

>>> myvar = 'test'
>>> def myfunc():
...     global myvar
...     exec('myvar = "changed!"')
...     print(myvar)
... 
>>> myfunc()
test
>>> print(myvar)
test

python global exec

21
推荐指数
2
解决办法
1万
查看次数

在函数内部使用exec设置变量

我刚开始自学Python,我需要一些关于这个脚本的帮助:

old_string = "didnt work"   
new_string = "worked"

def function():
    exec("old_string = new_string")     
    print(old_string) 

function()
Run Code Online (Sandbox Code Playgroud)

我想得到它old_string = "worked".

python string variables function exec

21
推荐指数
2
解决办法
1万
查看次数

如何使用C函数执行Shell内置命令?

我想通过像execv()这样的C语言函数执行Linux命令"pwd".

问题是没有一个名为"pwd"的可执行文件,我无法执行"echo $ PWD",因为echo也是一个没有可执行文件的内置命令.

c shell command exec pwd

20
推荐指数
3
解决办法
10万
查看次数