Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C-Linux-任何传递命令的方式”;历史";到Linux shell?_C_Linux_Shell - Fatal编程技术网

C-Linux-任何传递命令的方式”;历史";到Linux shell?

C-Linux-任何传递命令的方式”;历史";到Linux shell?,c,linux,shell,C,Linux,Shell,我正在写一个在Linux系统中运行的C程序。 程序将向shell传递一些Linux命令,并通过txt文件接收结果 system("last >> last.txt"); system("ls -s >>ls.txt"); 但我的“历史”失败了,因为有人告诉我,这不是命令,而是构建 那么,我有没有办法像其他人一样通过“历史” 谢谢 根据使用的shell,您可以查看历史文件 例如:~/.bash_history或~/.zsh_history。“history”相当于“cat

我正在写一个在Linux系统中运行的C程序。 程序将向shell传递一些Linux命令,并通过txt文件接收结果

system("last >> last.txt");
system("ls -s >>ls.txt");
但我的“历史”失败了,因为有人告诉我,这不是命令,而是构建

那么,我有没有办法像其他人一样通过“历史”


谢谢

根据使用的shell,您可以查看历史文件

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

“history”相当于“cat~/.bash_history”,因此aternative要使用:

system("cat ~/.bash_history");

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

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

将“bash”更改为首选shell。

谢谢!是否需要添加时间戳?这是一种输出格式?@JiayiGuo它只打印您在.bash_历史文件中记录的内容。如果要在输出中设置时间戳,则应提前设置名为HISTTIMEFORMAT的变量。只有在设置HISTTIMEFORMAT之后,新录制的命令才能附加时间戳。没关系。我设置了
HISTFILE=/dev/null
,因此它不会:)+1