944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 7728
  • Java RSS
Jun 7th, 2007
0

Implementing a unix shell running commands

Expand Post »
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:

Java Syntax (Toggle Plain Text)
  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. }


Java Syntax (Toggle Plain Text)
  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. }


Java Syntax (Toggle Plain Text)
  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. }


Java Syntax (Toggle Plain Text)
  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. }


Java Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ssimkhan is offline Offline
6 posts
since May 2007
Jun 7th, 2007
0

Re: Implementing a unix shell running commands

Check at java.sun.com/sourceforge.net. I think you can find some sample code.
Reputation Points: 769
Solved Threads: 128
Banned
ithelp is offline Offline
1,910 posts
since May 2006
Jun 7th, 2007
1

Re: Implementing a unix shell running commands

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.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: tokenization of file input
Next Thread in Java Forum Timeline: Sun Java Programmer Certification





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC