1

我正在编写一个在 Linux 系统中运行的 C 程序。该程序会将一些 Linux 命令传递给 shell 并接收带有 txt 文件的结果。

system("last >> last.txt");
system("ls -s >>ls.txt");

但是我在“历史”上失败了,因为有人告诉我这不是命令而是构建。

那么有什么办法可以像其他人一样传递“历史”吗?

谢谢!

4

3 回答 3

2

您还可以使用 shell 实际调用内置命令:

system("bash -e \"history >> history.txt\"");

更改"bash"为首选外壳。

于 2013-08-10T12:47:32.317 回答
1

"history" 等价于 "cat ~/.bash_history",所以一个替代方法是使用:

system("cat ~/.bash_history");
于 2013-08-10T12:45:36.013 回答
0

根据使用的外壳,您可以查看历史文件。

例如:~/.bash_history 或 ~/.zsh_history。

于 2013-08-10T12:44:41.150 回答