Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Java > Java and JDK > Pro Java
|
Pro Java Expert level Java questions not about a specific book. Please indicate your version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro Java section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 7th, 2007, 04:58 AM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help-Implementing a unix shell to run commands

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 file
import 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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XP shell commands... carumuga SQL Server 2005 2 July 24th, 2008 06:21 PM
where is shell script saved on unix box crmpicco Linux 2 August 3rd, 2007 12:56 PM
Help-Implementing a unix shell to run commands ssimkhan Java Basics 1 June 15th, 2007 06:43 AM
How to run DOS file commands using vba? alastair Access VBA 2 August 12th, 2006 03:40 AM
HTML with UNIX Commands Teqlump HTML Code Clinic 7 October 29th, 2004 02:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.