•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 426,126 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,680 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2765 | Replies: 2 | Solved
![]() |
•
•
Join Date: May 2007
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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:
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.
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) {}
}
}•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 939
Reputation:
Rep Power: 5
Solved Threads: 67
Check at java.sun.com/sourceforge.net. I think you can find some sample code.
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Overview of UNIX (Getting Started and Choosing a Distro)
- Execute Unix commands from Perl script (Perl)
- Making a UNIX Shell, so inexperienced at it (C++)
- Quick Reference for Linux Commands (Getting Started and Choosing a Distro)
- i want help about the Simulate a Linux/UNIX shell, called MASH in the C (C)
- Using find in a bash shell script (Shell Scripting)
Other Threads in the Java Forum
- Previous Thread: tokenization of file input
- Next Thread: Sun Java Programmer Certification



Linear Mode