You are on page 1of 3

Shell Commands

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Shell {


public static void main(String[] args) {
String input;
boolean loop=true;
Scanner sc=new Scanner(System.in);
System.out.println("Type 'help' to view list of commands");
while(loop){
String currentDir = "";
try{
currentDir = executeCommand("pwd").replace("\n",
">");
}catch(Exception e){}
System.out.print(currentDir);
input = sc.nextLine();
if(input.equals("exit"))
{
System.out.println("Exiting, Bye..");
loop = false;
break;
}
else if(input.equals("help"))
{
System.out.println("man\t\t\t:format and display the online manual pages");
System.out.println("ls\t\t\t:lists the contents of current
directory ");
System.out.println("ls -a\t\t\t:all entries and do not
ignore entries starting with .");
System.out.println("ls -r\t\t\t:lists the contents of current
directory in reverse order ");
System.out.println("ls -A\t\t\t:List all entries except for .
and ... Always set for the super-user");
System.out.println("ls -l\t\t\t:lists the contents of current
directory in reverse order ");
System.out.println("ls -c\t\t\t:Use time when file status
was last changed for sorting (-t) or long printing (-l).");
System.out.println("ls -ar\t\t\t: conjunction of flag 'a' and
'r'");
System.out.println("ls -lr\t\t\t: conjunction of flag 'l' and

'r'");
System.out.println("ls -Ar\t\t\t: conjunction of flag 'A' and
'r'");
System.out.println("ls -alr\t\t\t: conjunction of flag 'a', 'l'
and 'r'");
System.out.println("mkdir <dir name>\t:Creates new dir
");
System.out.println("rmdir <dir name>\t:remove
directories");
System.out.println("rm <dir entries>\t:remove directory
entries");
System.out.println("mv\t\t\t:moves file from one
directory to another");
System.out.println("cp\t\t\t:copy file from one directory
to another");
System.out.println("cp -r\t\t\t:copy directory from one
directory to another");
System.out.println("ping\t\t\t:pings a network hosts");
System.out.println("who\t\t\t:display who is logged in ");
System.out.println("who am I\t\t:Returns the invoker's
real user name.");
System.out.println("locate\t\t\t:find filenames quickly");
System.out.println("date\t\t\t:displays system Date ");
System.out.println("host\t\t\t:DNS lookup utility");
System.out.println("pwd\t\t\t:Present working directory
");
System.out.println("open\t\t\t:Opens files and
directories");
System.out.println("echo\t\t\t:write arguments to the
standard output");
System.out.println("printf\t\t\t:prints the formatted
output on screen");
System.out.println("cat\t\t\t:concatenate and print
files");
System.out.println("help\t\t\t:shows the set of
commands");
System.out.println("exit\t\t\t:Exits the shell ");
continue;
}
else{
try{
String output = executeCommand(input);
System.out.println(output);
}
catch(Exception e)
{
System.out.println("Command can not run");
}
}
}

}
public static String executeCommand(String command) throws Exception
{
StringBuffer output = new StringBuffer();
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new
InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine())!= null)
{
output.append(line + "\n");
}
return output.toString();
}
}

You might also like