C Linux shell - 重复最近的命令

时间:2022-06-29 00:49:51

I'm trying to implement a linux shell and in this part I'm trying to repeat the recent used commands. Here a sample from the code

我正在尝试实现一个linux shell,在这部分我试图重复最近使用过的命令。这是代码中的一个示例

if(strcmp(arg[0],"!") == 0 && arg[1] != NULL){
            if(strcmp(arg[1], "-1") == 0){
                 system("!!");


            }

I want the last command to execute when the user enters "! -1" I tried the system command "!!" but i got an error "command not found". Can you help please?

我希望在用户输入“! - 1”时执行最后一个命令我尝试了系统命令“!!”但我得到一个错误“命令未找到”。你能帮帮忙吗?

Thanks in advance

提前致谢

1 个解决方案

#1


1  

That is because !! is not really a command. It is a shortcut in bash to repeat the last recently used command. What's happening is that system is looking for !! binary file in your PATH environment variable and of course, failing to find it.

那是因为 !!不是一个真正的命令。它是bash中重写最近最近使用的命令的快捷方式。正在发生的是系统正在寻找!! PATH环境变量中的二进制文件,当然,无法找到它。

What you can do is what most shells do, keep a file of last used commands and execute the last one issued.

您可以做的是大多数shell所做的事情,保留最后使用的命令的文件并执行最后发布的命令。

For example, take a look at the file ~/.bash_history. That's the command history the console keeps track of for your user. In order to achieve what you want either you store the commands in a buffer in memory (not a very good idea because if you reset or close the shell you'll lose the history) or have them in a .bash_history-like file.

例如,看一下〜/ .bash_history文件。这是控制台为您的用户跟踪的命令历史记录。为了达到你想要的效果,你要么将命令存储在内存中的缓冲区中(这不是一个好主意,因为如果重置或关闭shell,你将丢失历史记录)或将它们放在类似.bash_history的文件中。

Also, take a look at the history command and mostly its manual page (man history). You'll find a section PROGRAMMING WITH HISTORY FUNCTIONS that you might find useful. By including <readline/history.h> you'll get access to some functions that operates on the history. I don't know if you can use it, because this might be only accessible within bash and since you're creating your own shell, maybe it won't work. I still think that keeping track of the history yourself is a way of KISS :)

另外,看看历史命令,主要是它的手册页(人类历史)。您将找到一个有用的历史功能编程部分。通过包含 ,您可以访问一些对历史记录进行操作的函数。我不知道你是否可以使用它,因为这可能只能在bash中访问,因为你正在创建自己的shell,也许它不会起作用。我仍然认为自己跟踪历史是KISS的一种方式:)

Hope this helps!

希望这可以帮助!

#1


1  

That is because !! is not really a command. It is a shortcut in bash to repeat the last recently used command. What's happening is that system is looking for !! binary file in your PATH environment variable and of course, failing to find it.

那是因为 !!不是一个真正的命令。它是bash中重写最近最近使用的命令的快捷方式。正在发生的是系统正在寻找!! PATH环境变量中的二进制文件,当然,无法找到它。

What you can do is what most shells do, keep a file of last used commands and execute the last one issued.

您可以做的是大多数shell所做的事情,保留最后使用的命令的文件并执行最后发布的命令。

For example, take a look at the file ~/.bash_history. That's the command history the console keeps track of for your user. In order to achieve what you want either you store the commands in a buffer in memory (not a very good idea because if you reset or close the shell you'll lose the history) or have them in a .bash_history-like file.

例如,看一下〜/ .bash_history文件。这是控制台为您的用户跟踪的命令历史记录。为了达到你想要的效果,你要么将命令存储在内存中的缓冲区中(这不是一个好主意,因为如果重置或关闭shell,你将丢失历史记录)或将它们放在类似.bash_history的文件中。

Also, take a look at the history command and mostly its manual page (man history). You'll find a section PROGRAMMING WITH HISTORY FUNCTIONS that you might find useful. By including <readline/history.h> you'll get access to some functions that operates on the history. I don't know if you can use it, because this might be only accessible within bash and since you're creating your own shell, maybe it won't work. I still think that keeping track of the history yourself is a way of KISS :)

另外,看看历史命令,主要是它的手册页(人类历史)。您将找到一个有用的历史功能编程部分。通过包含 ,您可以访问一些对历史记录进行操作的函数。我不知道你是否可以使用它,因为这可能只能在bash中访问,因为你正在创建自己的shell,也许它不会起作用。我仍然认为自己跟踪历史是KISS的一种方式:)

Hope this helps!

希望这可以帮助!