Look for the previous working directory for implementing "cd -"

I am currently using a limited functionality shell using the Java programming language. Shell volume is also limited. The challenge is to model the Unix shell as much as possible.

When I implement the cd command, I refer to the "Basic console commands" page , it mentions that the CD is able to return to the last directory in which I am located with the "cd -" command.

Since only the interface with the method is provided to me public String execute(File presentWorkingDirectory, String stdin).

I would like to know if there is an API call from Java that I can get from the previous working directory, or if there is any implementation for this command?

I know that one of the simple implementation is to declare a variable to store the previous working directory. However, currently I have the shell itself (the one that takes the command with the parameters), and every time a command tool is executed, a new thread is created. Therefore, I do not think that the "main" thread is recommended to be stored in the previous working directory.

Update (March 6 -14) : Thanks for the offer! Now I discussed with the encoder for the shell and added an additional variable to store the previous working directory. The following is a sample code for sharing:

public class CdTool extends ATool implements ICdTool {
    private static String previousDirectory;

    //Constructor
    /**
     * Create a new CdTool instance so that it represents an unexecuted cd command. 
     * 
     * @param arguments
     *  the argument that is to be passed in to execute the command
     */
    public CdTool(final String[] arguments) {
        super(arguments);
    }

    /**
     * Executes the tool with arguments provided in the constructor
     * 
     * @param workingDir
     *            the current working directory path
     * 
     * @param stdin
     *            the additional input from the stdin
     * 
     * @return the message to be shown on the shell, null if there is no error
     *         from the command
     */
    @Override
    public String execute(final File workingDir, final String stdin) {
        setStatusCode(0);
        String output = "";

        final String newDirectory;

        if(this.args[0] == "-" && previousDirectory != null){
            newDirectory = previousDirectory;
        }
        else{
            newDirectory = this.args[0];
        }

        if( !newDirectory.equals(workingDir) &&
            changeDirectory(newDirectory) == null){
            setStatusCode(DIRECTORY_ERROR_CODE);
        output = DIRECTORY_ERROR_MSG;
    }
    else{
        previousDirectory = workingDir.getAbsolutePath();
        output = changeDirectory(newDirectory).getAbsolutePath();
    }

    return output;
}

}

PS: Please note that this is not a complete implementation of the code, and this is not the full functionality of cd.

+3
source share
2 answers

Real shell ( Bash) PWD OLDPWD. PWD , OLDPWD , cd - .

:

cd /tmp
echo "$OLDPWD"          # /home/palec
export OLDPWD='/home'
cd -                    # changes working directory to /home

, ( , , , ), , .

, ( ). . , , . cd ( ) .

+2

, LinkedList, presentWorkingDirectory , , linkedList.popLast, .

+1

All Articles