-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExecuteShellCommand.java
75 lines (54 loc) · 1.87 KB
/
ExecuteShellCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package ChatBot;
import java.io.*;
public class ExecuteShellCommand {
String key="";
public ExecuteShellCommand(String key)
{
this.key=key;
}
public void executeCommands() throws IOException, InterruptedException {
File tempScript = createTempScript();
try {
ProcessBuilder pb = new ProcessBuilder("bash", tempScript.toString(),key);
pb.inheritIO();
Process process = pb.start();
process.waitFor();
} finally {
tempScript.delete();
}
}
public String executeCommand(String command)
{
{
StringBuffer output = new StringBuffer();
Process p;
try {
p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader =
new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null) {
output.append(line + "\n");
}
} catch (Exception e) {
e.printStackTrace();
}
return output.toString();
}
}
public File createTempScript() throws IOException {
File tempScript = File.createTempFile("script", null);
Writer streamWriter = new OutputStreamWriter(new FileOutputStream(
tempScript));
PrintWriter printWriter = new PrintWriter(streamWriter);
printWriter.println("cp /home/ram_aditya/eclipse-workspace/ChatBot/src/ChatBot/new /home/ram_aditya/eclipse-workspace/ChatBot/temp");
printWriter.println("sed 's/<[^>]\\+>/ /g' temp > new1");
printWriter.println("grep -i $1.* new1 | sed '/{/d' - |tr -s '\t ' | sed -n -f instr | head -n4 > new2");
printWriter.println("sed -i '/disambiguation/d' new2");
printWriter.println("sed -i -f instr2 new2");
printWriter.println("cat new2");
printWriter.close();
return tempScript;
}
}