Java Code For The Above Problem (Since you Haven't mentioned preferable language hence coding in JAVA, other language solution can also be made on the above problem and let me know for any type of help)
NOTE -- I'm using WINDOWS and Command Prompt (cmd shell) to run the system call. You can also use linux or other system to execute it, than just change the first argument of Process Builder with your executable shell (bash e.t.c) .
Comments are provided in the code for better understandability, ask in comments for any doubt.
---------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
public class Command {
public static void main(String[] args) throws
Exception {
// File Object of System Call
File fileObject = new
File("mentioned file path here");
// Using Buffered Reader to read it
from File
@SuppressWarnings("resource")
BufferedReader bufferedReader = new
BufferedReader(new FileReader(fileObject));
// Let string represents String --
Temporary String to Read Line
@SuppressWarnings("unused")
String string;
//Output File name
capture.txt
FileWriter outPutFile = new
FileWriter("capture.txt");
//ProcessBuilder to Build the
Process or System Call
//Here arguments 1st represent
'Console' were we are running the Program.
//Since I'm using CMD that's why
used cmd.exe
ProcessBuilder builder;
//Read All the OutPut File
while((string =
bufferedReader.readLine()) != null){
//Here your
string can also include file Directory Path ... Thn string will
represent like this
//
file_Directory_Path && command
builder = new
ProcessBuilder(
"cmd.exe", "/c", "dir");
builder.redirectErrorStream(true);
// start the
executing Command
Process p = builder.start();
//Output from the above executing
File
BufferedReader outPutExecCode = new
BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line =
outPutExecCode.readLine();
if (line == null) { break; }
//write into File
outPutFile.write(line);
// System.out.println(line); //Just
to ensure in Command Prompt
}
}
//Closing File Object
outPutFile.close();
}
}
------------------------------------------------------------------------------------------------------------------------------------------------
Output -- I have used hard coded input to test the code, different system call or command were used to test the code and it works fine with all system command.
Screenshot of one output is attached (dir command in cmd.exe works as ls of bash and shell)
{I have cmd.exe in my laptop hence tested with cmd.exe only}
File Output (i.e capture.txt for the same)
2. capture This solution to this problem will require using some system calls that will be...