Connecting Tech Pros Worldwide Forums | Help | Site Map

create a shell using java

Newbie
 
Join Date: Oct 2009
Posts: 1
#1: Oct 9 '09
i have to implement the shell such that it accepts commands, together with parameters. Assume all possible commands have either 0 or 1 parameter. I need to check this.
if(commandstrings.length>2) {output error message and continue to the beginning of the loop}

Note: the continue statement will skip all the following lines and go back to the beginning of the while loop.
In the skeleton, commandLine=console.readLine(); reads in a command and assigns it to the commandLine string. If the command has an argument, then the command and the argument will be separated by a space, but in a single string. However, ProcessBuilder cannot use such a string. When executing “new ProcessBuilder()”, the constructor takes in 1 or more strings as arguments. The first should be the command string, and the second, the argument string, if there is an argument. This means we have to separate the commandLine into its individual components. This could be done as follows:
commands=commandLine.split(“ “); i.e. split the commandLine string using a space as a separator.
The individual component strings are stored in commands, which should be an array of strings. Thus, if the user entered the following command at the jsh> prompt: ls -la, commands*0+ will have “ls” and commands*1+ will have “-la”.
One can then do pb=new ProcessBuilder(commands[0],commands[1]).
Then ProcessBuilder’s directory method can be used to set the working directory, as you did in the lab exercise e.g. pb.directory(new File(workingdirectory));

YarrOfDoom's Avatar
Expert
 
Join Date: Aug 2007
Location: Belgium
Posts: 1,120
#2: Oct 9 '09

re: create a shell using java


Could you please bring some structure into the question? It's quite hard to understand what exactly you are asking when no question is clearly defined.
It would also be quite helpful if you posted the code you have so far, instead of describing it. If you post code, you can put [code]-tags around it ( [code] in front of it, and [/code] behind it).

I see it's your first post here, so here's a link to the FAQ & Posting Guidelines, which contain a lot of useful information for getting the best out of this forum.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#3: Oct 10 '09

re: create a shell using java


@gisto moss: are you also JavaNut13 on the Sun Java forums? If so: people don't like crossposting.

kind regards,

Jos
Reply