the program must alllow users to rerun commands from their h
the program must alllow users to rerun commands from their history by supporting the following three techniques.
when the user enters the command history you will print out the contents of the history of commands that have been entered into the shell.
when the user enters !!, run the previous command in the history.
when the user enters !<interger value i> run the ith command in the history
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class test {
public static String windows_separator = \"\\\\\";
public static String linux_separator = \"/\";
public static void main(String[] args) throws java.io.IOException {
String osName=System.getProperty (\"os.name\");
String separator=linux_separator;
if(osName.startsWith(\"W\")){
separator=windows_separator;
}
String commandLine;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
ProcessBuilder pb = new ProcessBuilder();
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
while (true) {
try{
// user input
System.out.print(\"jsh>\");
commandLine = console.readLine();
// If they entered a return, just loop again
if (commandLine.equals(\"\")) {
continue;
}
// Now parse the input to obtain the commands and params
// Create a StringTokenizer object and ArrayList object
StringTokenizer st = new StringTokenizer(commandLine);
ArrayList<String> myCommands = new ArrayList<String>();
// Store commands
while (st.hasMoreTokens()) {
myCommands.add(st.nextToken());
}
if (myCommands.contains(\"cd\")) {
if (myCommands.get(myCommands.size() - 1).equals(\"cd\")) {
File home = new File(System.getProperty(\"user.home\"));
System.out.println(\"your home directory is \" + home);
pb.directory(home);
continue;
} else {
String argument = myCommands.get(myCommands.size() - 1);
System.out.println(\"argument passed is \" + argument);
String createPath = pb.directory() + separator + argument;
System.out.println(\"path created is \" + createPath);
File newPath = new File(createPath);
pb.directory(newPath);
continue;
} // else
} // check
// Create the Process builder object
pb.command(myCommands);
// Now start the process
Process process = pb.start();
// Obtain the output stream
is = process.getInputStream();
// Read what the process returned
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
// Content returned by the command
System.out.println(line);
}
}catch(Exception exception){
System.out.println(exception.getMessage());
}finally{
//Close the resources after use
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}
}
}
}
Solution
package chegg;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class CommandHistory {
public static String windows_separator = \"\\\\\";
public static String linux_separator = \"/\";
public static void main(String[] args) throws java.io.IOException {
ArrayList<ArrayList> history = new ArrayList<ArrayList>();
String osName=System.getProperty (\"os.name\");
String separator=linux_separator;
if(osName.startsWith(\"W\")){
separator=windows_separator;
}
String commandLine;
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
ProcessBuilder pb = new ProcessBuilder();
InputStream is=null;
InputStreamReader isr=null;
BufferedReader br=null;
while (true) {
try{
// user input
System.out.print(\"jsh>\");
commandLine = console.readLine();
// If they entered a return, just loop again
if (commandLine.equals(\"\")) {
continue;
}
// Now parse the input to obtain the commands and params
// Create a StringTokenizer object and ArrayList object
StringTokenizer st = new StringTokenizer(commandLine);
ArrayList<String> myCommands = new ArrayList<String>();
// Store commands
String cmd=\"\";
while (st.hasMoreTokens()) {
myCommands.add(st.nextToken());
}
history.add(myCommands);
cmd = myCommands.get(0);
if (myCommands.contains(\"cd\")) {
if (myCommands.get(myCommands.size() - 1).equals(\"cd\")) {
File home = new File(System.getProperty(\"user.home\"));
System.out.println(\"your home directory is \" + home);
pb.directory(home);
continue;
} else {
String argument = myCommands.get(myCommands.size() - 1);
System.out.println(\"argument passed is \" + argument);
String createPath = pb.directory() + separator + argument;
System.out.println(\"path created is \" + createPath);
File newPath = new File(createPath);
pb.directory(newPath);
continue;
} // else
} // check
if (cmd.equals(\"!!\")) {
printHistory(history);
continue;
}
else if (cmd.contains(\"!\")) {
System.out.println(\"Execute command\");
int number = Integer.parseInt(cmd.substring(1, cmd.length()));
myCommands = history.get(number);
if (myCommands.get(0).equals(\"!!\")) {
printHistory(history);
continue;
}
}
// Create the Process builder object
pb.command(myCommands);
// Now start the process
Process process = pb.start();
// Obtain the output stream
is = process.getInputStream();
// Read what the process returned
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
// Content returned by the command
System.out.println(line);
}
}catch(Exception exception){
System.out.println(exception.getMessage());
}finally{
//Close the resources after use
if(is!=null)
is.close();
if(isr!=null)
isr.close();
if(br!=null)
br.close();
}
}
}
static void printHistory(ArrayList<ArrayList> history) {
System.out.println(\"Print history\");
for (int i=0;i<history.size();i++) {
ArrayList<String> incmd = history.get(i);
String overallcmd = \"\";
for (int j=0;j<incmd.size();j++) {
overallcmd = overallcmd +incmd.get(j);
}
System.out.println(overallcmd);
}
}
}



