View Single Post
  #1 (permalink)  
Old June 6th, 2007, 10:01 AM
ssimkhan ssimkhan is offline
Registered User
 
Join Date: May 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help-Implementing a unix shell to run commands

I'm new to Java Programming and 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:

//To create and write text to a file:
import java.lang.*;
import java.io.*;

public class Create
{
    String command="";
    BufferedReader br;
    BufferedWriter out;
    File file = new File("f1");

    public void createFile()
    {
        try {
            //File file = new File("f1");

            // Create file if it does not exist
            boolean success = file.createNewFile();
            if (success)
            {
            writeFile();
            }
            else
            {
                 System.out.println("file already exists");

            }
        } catch (IOException e) {}
    }


    public void writeFile()
    {
        try {

            out = new BufferedWriter(new FileWriter(file));
            out.write("You have passed \n");
            out.write("The Exams");
            out.close();
        } catch (IOException e) {}
    }

}


//To read file
import java.lang.*;
import java.io.*;

public class Read
{
    BufferedReader br;
    BufferedWriter out;
    public void readFile()
    {
        try {
            br = new BufferedReader(new FileReader("f1"));
            String Str;
            while ((Str = br.readLine()) != null)
            {
                   System.out.println(Str);
            }
            br.close();

        } catch (IOException e) {}
    }
}


//To copy file
import java.lang.*;
import java.io.*;

public class Copy
{
    public void copyFile()
    {
        try{
            InputStream in = new FileInputStream("f1");
            OutputStream out = new FileOutputStream("f2");

            // Transfer bytes from in to out
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0)
            {
                out.write(buf, 0, length);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
        }catch(IOException e) {}
    }
}


//To remove fileimport java.lang.*;
import java.io.*;

public class Remove
{
    public void removeFile(File file)
    {
        boolean success = file.delete();
        if (!success)
        {
            System.out.println("File not deleted");
        }
    }
}

//To check for substring

import java.lang.*;
import java.io.*;

public class Sub
{

    BufferedReader br;
    BufferedWriter out;

    public void Search()
    {
        try {
            br = new BufferedReader(new FileReader("f1"));
            String Str;
            while ((Str = br.readLine()) != null)
            {
                   System.out.println(Str);

                   boolean found = true;

                   int index = Str.indexOf("have");
                   if(found)
                   {

                    System.out.println("Substring found");
                    System.out.println(br.readLine());
                }
                else
                    System.out.println(" ");
            }
            br.close();

        } catch (IOException e) {}
    }


}

Please help me to put all this together using threads. Im looking forward to any one's reply.Thanks in advance.
Reply With Quote