简体   繁体   中英

Can I get executed commands not using history or ~/.bash_history?

My code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(void)
{
    printf("entering main process---\n");
    int ret;
    char *argv[] = {"history",NULL};
    ret = execvp("history",argv);
    if(ret == -1) 
        perror("execl error");
    printf("exiting main process ----\n");
    return 0;
}

Output: entering main process--- execl error: No such file or directory exiting main process ----

Question: Can I get executed commands not using history or ~/.bash_history?

It seems that something is wrong using function like execvp . I've tried system function.

Code:

#include <stdio.h>
int main()
{
    system("history");
    return 0 ; 
}

Nothing output.

If you try a man history you will get into the BASH_BUILTINS(1) General Commands Manual page. This means history is part of the bash shell internals. In order to have something executed via execvp() you need to have an actual executable somewhere in your PATH .

It's unclear why reading ~/.bash_history is not enough. Is it perhaps because you want the history of the currently running shell?

The short answer is no, you can't get it.

The long answer is you could attach with ptrace or through /proc/pid/mem , find the history in memory, and print it. Probably not worth the effort.

You can pipe the output of the history builtin if you wish, by running your program with

history | ./myprog

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