Implementing a unix shell running commands

Thread Solved

Join Date: May 2007
Posts: 6
Reputation: ssimkhan is an unknown quantity at this point 
Solved Threads: 0
ssimkhan ssimkhan is offline Offline
Newbie Poster

Implementing a unix shell running commands

 
0
  #1
Jun 7th, 2007
I'm trying to do an assignment to implement a unix shell and run commands using java. commands include read file, copy file, remove file and find substring in a file. I have managed to create the file and try out all commands individually but I've failed to run the threads. Below is a suggestion and code for individual commands:

Suggestion
The purpose of this assignment is to introduce you to Java programming. You are to implement a simple shell (command interpreter) that behaves similarly to the UNIX shell. When you type in a command (in response to its prompt), it will create a thread that will execute the command you entered.
Multiple commands can be chained together on a single line, The commands can either be separated by `&' (ampersand) or `;'.

The public static void main() procedure in your primary class will be quite simple. It will be an infinite loop that prints a prompt, reads a line (in other courses, a program with an infinite loop is considered a bad thing, but in Operating systems, it's the norm!), parses it (breaks it up into its constituent commands), either starts a new thread to handle each of the different commands if they are chained by `&', or starts the threads one by one if they are chained by ';', and then waits for all the threads to finish before printing the next prompt.


code:

  1. //To create and write text to a file:
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Create
  6. {
  7. String command="";
  8. BufferedReader br;
  9. BufferedWriter out;
  10. File file = new File("f1");
  11.  
  12. public void createFile()
  13. {
  14. try {
  15. //File file = new File("f1");
  16.  
  17. // Create file if it does not exist
  18. boolean success = file.createNewFile();
  19. if (success)
  20. {
  21. writeFile();
  22. }
  23. else
  24. {
  25. System.out.println("file already exists");
  26.  
  27. }
  28. } catch (IOException e) {}
  29. }
  30.  
  31.  
  32. public void writeFile()
  33. {
  34. try {
  35.  
  36. out = new BufferedWriter(new FileWriter(file));
  37. out.write("You have passed \n");
  38. out.write("The Exams");
  39. out.close();
  40. } catch (IOException e) {}
  41. }
  42.  
  43. }


  1. //To read file
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Read
  6. {
  7. BufferedReader br;
  8. BufferedWriter out;
  9. public void readFile()
  10. {
  11. try {
  12. br = new BufferedReader(new FileReader("f1"));
  13. String Str;
  14. while ((Str = br.readLine()) != null)
  15. {
  16. System.out.println(Str);
  17. }
  18. br.close();
  19.  
  20. } catch (IOException e) {}
  21. }
  22. }


  1. //To copy file
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. public class Copy
  6. {
  7. public void copyFile()
  8. {
  9. try{
  10. InputStream in = new FileInputStream("f1");
  11. OutputStream out = new FileOutputStream("f2");
  12.  
  13. // Transfer bytes from in to out
  14. byte[] buf = new byte[1024];
  15. int length;
  16. while ((length = in.read(buf)) > 0)
  17. {
  18. out.write(buf, 0, length);
  19. }
  20. in.close();
  21. out.close();
  22. System.out.println("File copied.");
  23. }catch(IOException e) {}
  24. }
  25. }


  1.  
  2. //To remove file
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. public class Remove
  7. {
  8. public void removeFile(File file)
  9. {
  10. boolean success = file.delete();
  11. if (!success)
  12. {
  13. System.out.println("File not deleted");
  14. }
  15. }
  16. }


  1. //To check for substring
  2.  
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. public class Sub
  7. {
  8.  
  9. BufferedReader br;
  10. BufferedWriter out;
  11.  
  12. public void Search()
  13. {
  14. try {
  15. br = new BufferedReader(new FileReader("f1"));
  16. String Str;
  17. while ((Str = br.readLine()) != null)
  18. {
  19. System.out.println(Str);
  20.  
  21. boolean found = true;
  22.  
  23. int index = Str.indexOf("have");
  24. if(found)
  25. {
  26.  
  27. System.out.println("Substring found");
  28. System.out.println(br.readLine());
  29. }
  30. else
  31. System.out.println(" ");
  32. }
  33. br.close();
  34.  
  35. } catch (IOException e) {}
  36. }
  37.  
  38.  
  39. }
Please help me to put all this together using threads. I've tried the main class but its really giving problems. Im looking forward to any one's reply.Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,826
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Implementing a unix shell running commands

 
0
  #2
Jun 7th, 2007
Check at java.sun.com/sourceforge.net. I think you can find some sample code.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,438
Reputation: Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of Ezzaral has much to be proud of 
Solved Threads: 510
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: Implementing a unix shell running commands

 
1
  #3
Jun 7th, 2007
Well, since it is homework, I'll just give a general tip and let you figure the rest. Make each of your operation classes extend Thread. This means they must all implement a "run()" method. I think you can figure out how that applies to what you are aiming for.

I would also suggest that the one thing all of these do is operate on a file, so you may consider this a good thing to pass into the constructor.

For the main method, read the tutorials on basic thread handling. It should be fairly straight forward from there.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC