Java 查找以前的工作目录以实现;cd-“;

Java 查找以前的工作目录以实现;cd-“;,java,shell,directory,cd,Java,Shell,Directory,Cd,我目前正在使用Java编程语言实现一个功能有限的shell。外壳的范围也有限制要求。任务是尽可能多地为Unix shell建模 当我实现cd命令选项时,我引用了一个,它提到一个cd能够返回到我使用命令“cd-”的最后一个目录 因为我只得到了一个带有方法publicstringexecute(File presentWorkingDirectory,stringstdin)的接口 我想知道是否有来自Java的API调用,我可以检索以前的工作目录,或者是否有此命令的任何实现 我知道一个简单的实现是声

我目前正在使用Java编程语言实现一个功能有限的shell。外壳的范围也有限制要求。任务是尽可能多地为Unix shell建模

当我实现cd命令选项时,我引用了一个,它提到一个cd能够返回到我使用命令“cd-”的最后一个目录

因为我只得到了一个带有方法
publicstringexecute(File presentWorkingDirectory,stringstdin)的接口

我想知道是否有来自Java的API调用,我可以检索以前的工作目录,或者是否有此命令的任何实现

我知道一个简单的实现是声明一个变量来存储以前的工作目录。但是,我目前拥有shell本身(接受带有选项的命令的shell),每次执行命令工具时,都会创建一个新线程。因此,我认为“主”线程不应该存储以前的工作目录

更新(2014年3月6日):感谢您的建议!我现在已经与shell的编码器进行了讨论,并添加了一个额外的变量来存储以前的工作目录。以下是共享的示例代码:

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

}
注:请注意,这不是代码的完整实现,这也不是cd的全部功能

realshell(至少Bash)shell在
PWD
环境变量中存储当前工作目录路径,在
OLDPWD
中存储旧工作目录路径。重写
PWD
不会改变您的工作目录,但是重写
OLDPWD
确实会改变
cd-
将带您去的地方

试试这个:

cd/tmp
echo“$OLDPWD”#/home/palec
导出OLDPWD='/home'
cd-#将工作目录更改为/home
我不知道如何实现shell功能(即如何表示当前工作目录;通常它是进程的固有属性,由内核实现),但我认为必须将旧的工作目录保留在一个额外的变量中。


顺便说一下,shell还为执行的每个命令(内部命令除外)分叉。当前工作目录是进程的属性。当命令启动时,它可以更改其内部当前工作目录,但不会影响shell的工作目录。仅
cd
命令(内部)可以更改shell的当前工作目录。

如果要保留多个工作目录,只需创建一个LinkedList,将每个新的presentWorkingDirectory添加到,如果要返回,请使用LinkedList.popLast获取最后一个工作目录。

如果只想返回一个级别,请使用变量,如果你想有更多的级别,保持一个堆栈。我认为@Kent在问题下面的评论中提出的解决方案更好。您应该使用堆栈来实现这样的功能。堆栈是基于数组的,所以它没有指针(在本例中是引用)和节点分配的开销(可能它们是预先分配的;我不太了解Java的胆量)。如果您使用ArrayList,那么它实际上是一样的。我只是觉得LinkedList非常有用,因为列表的长度可能会有很大的不同,LinkedList比基于数组的列表/堆栈要好。对不起,我忘记了堆栈应该包含什么。事实上,当记住工作目录更改的历史时,链表是最好的解决方案。如果想要保留所有的历史记录——当只需要固定数量的最后工作目录时,最好使用循环缓冲区(基于数组的deque)。