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
8 changes: 8 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,21 @@
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
ProcessBuilder pb = new ProcessBuilder("sh","run.sh");
pb.directory(getWorkingDir());
Map<String,String> envs = pb.environment();
envs.put("AAA","123");

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.

为什么又把不必要的文件提交上来?

total 18K
-rw-r--r-- 1 胡凯波 197609 12 8月 16 12:47 output.txt
drwxr-xr-x 1 胡凯波 197609 0 8月 16 12:46 ..
-rwxr-xr-x 1 胡凯波 197609 50 8月 16 12:33 run.sh
drwxr-xr-x 1 胡凯波 197609 0 8月 16 12:33 .
-rw-r--r-- 1 胡凯波 197609 11K 8月 16 12:33 .hidden.txt