Question

2. capture This solution to this problem will require using some system calls that will be discussed in week 3. This program should accept the name of a file that includes a list of commands (one per line) to execute. Each line of the file should be executed by a subprocess and the output written to the file capture.txt For example, given the command file: /bin/ls-1 /bin/cat apple banana usr/bin/wc -1-wcanteloupe The output saved to capture.txt might be: W1 schubert rw--1 schubert them 51 Jan 18 12:07 apple rw-1 schubert them 27 Jan 18 12:09 banana rw--1 schubert them 13179 Jan 18 12:15 cantaloupe first line of file apple second line of file apple banana first and only line

unlink open close dup read write lseek remove a file open a file for reading/writing close a previously open file duplicate an existing file descriptor extract info from a file put info into a file move to specified location in a file fork execl wait exit dup, dup2 duplicate an existing file descriptor kill create a new almost-copy of the current program replace program being executed with a new one wait for child process to complete terminate self Send a signal to a process To send a signal: To handle a signal signal (sig, handler) kill (pid, sig)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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}

hello yogi Pizzallouse Find AllActivate. //Read All the Oli t P1t İle //while( (atringbufferedreader.readLine )) !- null) ( 30 (default package) //Here your string can also include file Directory Path. Thn string O || ⓘ Connect Mylyn 32 filo Diroctory Path command builder-new ProceasBilder 》斷JRE System Library [JavaSE 1.8 Connect to your task and ALM toc creale a local lask. and.exe, , dir; 35 builder.rediretErrorStreaut. (true / start the executing command Procesa p - builder.atart): //Output ro the above executing File BufferedReader outFutExecCodenew BufferedReader (new InputstreamRea String line; e, Command 39 while (true) t line outPutExecCode.readLine ProbleravadcDeclaraionConsole rle rinated> Command卩dva Application C:\Prograrn FilesVavayre1.80191biyavaw.exe (Jan 21, 2019, 11:58:41 P Volume in drive D ha no label. Volune Serial Nunber is C3E0-5242 Directory of D:CheggPracticeizzaHouse 01/21/2019 11:56 FM <DIR 01/21/2019 1:5 PM <DIR> 12/29/2018 02:50 PM 12/29/2019 02:50 EM 12/29/2010 02:50 FM <DIR> 01 21/2019 11:12 PM<DIR> 01/21/2019 11:58 IM 01/21/2019 11:12 FM <DIR 301 .classpath 386 project settings bin 0 capture.txt 3 File (s) 5 Dir (a) 257,165, 885, 440 bytes free 687 bytesFile Output (i.e capture.txt for the same)

ile dit Selection Find View Goto lools Project Preferences Help capture.bxt 1 Volume in drive D has no label. Volume Serial Number is C3E0-5242 Directory of D: CheggPracticelPizzaHouse01/21/2019 11:56 PM la . .12/29/2818 02:50 PM DIR> PM 11:58 PM bytes .01/21/2019 11:56 PM <DIR> 301.classpath12/29/2018 02:50 386 project12/29/2018 02:50 PM <DIR> .settings01/21/2019 11:12 PM <DIR> bin01/21/2019 687 0 capture.txt01/21/2019 11:12 PM <DIR src 3 File(s) 5 Dir(s) 257,165,885,440 bytes free

Add a comment
Know the answer?
Add Answer to:
2. capture This solution to this problem will require using some system calls that will be...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT