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));