Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/main/java/com/github/hcsp/shell/Fork.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@
import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;

public class Fork {
public static void main(String[] args) throws Exception {
// 请在这里使用Java代码fork一个子进程,将fork的子进程的标准输出重定向到指定文件:工作目录下名为output.txt的文件
// 工作目录是项目目录下的working-directory目录(可以用getWorkingDir()方法得到这个目录对应的File对象)
// 传递的命令是sh run.sh
// 环境变量是AAA=123

//1、可行性程序,2、参数
ProcessBuilder pb=new ProcessBuilder("sh","run.sh");
// 3、工作路径、目录
pb.directory(getWorkingDir());
//4、环境变量
Map<String, String> environment = pb.environment();
environment.put("AAA","123");

// pb.inheritIO();
pb.redirectOutput(getOutputFile());

pb.start().waitFor();

}

private static File getWorkingDir() {
Expand Down
7 changes: 7 additions & 0 deletions working-directory/output.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
AAA is: 123

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你多提交了一个文件上来。。。

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

发现了,没审题,下次注意!
这个我可以直接删掉吗?

total 18K
-rw-r--r-- 1 acton 197609 12 8�� 15 11:40 output.txt
drwxr-xr-x 1 acton 197609 0 8�� 15 11:40 ..
drwxr-xr-x 1 acton 197609 0 8�� 14 18:14 .
-rw-r--r-- 1 acton 197609 11K 8�� 14 18:14 .hidden.txt
-rwxr-xr-x 1 acton 197609 50 8�� 14 18:14 run.sh