简体   繁体   中英

C-Linux-Any way to pass command “history” to Linux shell?

I'm writing a C program to run in Linux system. The program will pass some Linux commands to shell and receive the result with txt files.

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

But I failed with "history" as someone told me that this is not a command but a buildin.

So is there any way I can pass "history" as other ones?

Thanks!

You might also be able to actually invoke the built-in command by using the shell:

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

Change "bash" to the preferred shell.

“history”相当于“cat~ / .bash_history”,所以替代方法是使用:

system("cat ~/.bash_history");

Depending on the shell used you can look at the history file.

For example: ~/.bash_history or ~/.zsh_history.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM