-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExternalCmd.java
More file actions
53 lines (47 loc) · 1.8 KB
/
ExternalCmd.java
File metadata and controls
53 lines (47 loc) · 1.8 KB
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mycompany.seven.bii;
/**
*
* @author 7 Bii
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.Arrays;
public class ExternalCmd {
static String outline = "";
public static String externalCmdSort(String input) throws IOException {
//Checks list of commands
while (!false){
String [] parsedCommand = input.split(" ");
String[] listOfCommands = {"ls", "cp", "mv", "mkdir", "rmdir", "ps", "which"};
boolean valid = Arrays.stream(listOfCommands).anyMatch(Array.get(parsedCommand, 0)::equals);
//if not a command returns false
if ( !valid ){
return "false";
}else {
//if command found uses process
ProcessBuilder pb = new ProcessBuilder();
pb.command(parsedCommand);
pb.directory(new File(System.getProperty("user.dir"))); //sets process builder directory to current directory
Process process = pb.start();
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
outline="";
while ((line = reader.readLine()) != null) {
//prints output
outline +=" "+line;
System.out.println(outline);
}
}
}return outline;
}
}
}