You will develop a simple UNIX shell (command interpreter). Your shell will impl
ID: 3758654 • Letter: Y
Question
You will develop a simple UNIX shell (command interpreter). Your shell will implement the following commands: HAS TO BE IN JAVA PLEASE. THE CODES ARE AT THE BOTTOM.
cat file1 [file2 file3 ...]
Output the contents of one or more files
grep searchString
Read lines from piped input and output only those lines that contain searchString
lc
Read lines from piped input and output a single number after reading all input lines. This number must be the number of lines in the input (which may be zero).
history
Displays the list of commands executed so far. Similar to the cat command, history will always be first in a string of commands separated by pipes.
!n
n is the number of one of the commands executed by the user in the past and you want to execute again as it appears in the history
pwd
Output the path of the current working directory
ls
Output the lists of the files in the current working director
cd
Change to another directory relative to the current directory
exit
Quit the command line.
MyShell.java
import java.io.*;
import java.util.Scanner;
import java.util.regex.*;
public class myShell {
public static void main (String args[]) {
/* Create Scanner object for to read in command line */
Scanner getCommands = new Scanner(System.in);
/* String to store command line input */
String commandLine;
/* Flag to allow looping for additional command lines */
boolean loopFlag = true; /
* Loop through commands on command line */
try {
while(loopFlag) {
/* Display myShell cursor */
System.out.print(" myShell> ");
/* Get new command line input */
commandLine = getCommands.nextLine();
/* Break up commands and store them in an array */
String[] commandArray = commandLine.split(";");
/* Create a thread array for each command detected */
cmdThread[] thread = new cmdThread[commandArray.length];
/* Loop to allow a new thread per command to be created */ f
or(int i = 0; i < commandArray.length; i++) {
/** Your work starts here! */
}
/* Join the threads */
for(int i = 0; i < commandArray.length; i++) {
try
{
thread[i].join();
}
/* Thrown when a thread is waiting, sleeping, or otherwise occupied, and the thread is interrupted, either before or during the activity. */
catch (InterruptedException e)
{
}
}
}
}
/* Check for a keyboard interrupt or other non-serious system problem */
catch (Exception e) {
System.out.println(" Interrupt was detected. myShell closing.");
System.exit(0); }
}
}
Explanation / Answer
import java.util.Scanner; /** * A Java Scanner class example from http://alvinalexander.com */ public class JavaScannerExample { public static void main (String[] args) { // create a scanner so we can read the command-line input Scanner scanner = new Scanner(System.in); // prompt for the user's name System.out.print("Enter your name: "); // get their input as a String String username = scanner.next(); // prompt for their age System.out.print("Enter your age: "); // get the age as an int int age = scanner.nextInt(); System.out.println(String.format("%s, your age is %d", username, age)); } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.