Find the answer to your Linux question:
Results 1 to 10 of 10
hi i want to execute the history command in c wih this simple code but it dosnot show anything,any other solution.. Code: #include <stdio.h> main() { system("history > a.txt"); } ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14

    problem with callig history command in c file


    hi
    i want to execute the history command in c wih this simple code but it dosnot show anything,any other solution..
    Code:
    #include <stdio.h>
    main()
    {
      system("history > a.txt");
    }
    my purpose is to write the history log file on a txt file with a c program..
    pls help me if you have any idea..
    thanks.

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    Where the history is stored depends upon the shell, and is different for each invocation of the shell. For the bash shell, look in the ~/.bash_history file.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14
    i find ~/.bash_history but the problem is that i need the timestamp of each entry so i added this: export HISTTIMEFORMAT="%F %T " ,
    this will show me the timestamp in the bash but the change is not applied to ~/.bash_history.it means that there is no timestamp in ./bash_history.
    so that 's why i went for system("history > a.txt");
    any possibility that i have timestamp in ~/.bash_history???
    thanks..

  4. $spacer_open
    $spacer_close
  5. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,695
    Sorry, I don't know. I'd have to look at the bash source code to see exactly what is going on with history stuff. I don't see any time stamps in ~/.bash_history, but I tried the environment variable that you showed and it does output a timestamp along with the sequence number. I'm not sure how they are doing that unless it is coming from somewhere other than ~/.bash_history. Maybe one of the other lurkers here know and can comment?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  6. #5
    Just Joined! jippie's Avatar
    Join Date
    May 2006
    Location
    Eindhoven, the Netherlands
    Posts
    76
    Did you try this:

    system('HISTTIMEFORMAT="%F %T " history > a.txt');

  7. #6
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Has my post in t'other forum answered your question?

    If your problem is resolved, please say so in any threads you have started so people don't waste their time trying to help you out.

  8. #7
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14
    sorry,but i could not get my answer!!!

  9. #8
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14
    Quote Originally Posted by jippie View Post
    Did you try this:

    system('HISTTIMEFORMAT="%F %T " history > a.txt');
    it did not work.

  10. #9
    Just Joined!
    Join Date
    Jan 2010
    Posts
    40
    I've run shell builtin called "set" and it revealed essential differences between the shell I work and the (probably temporary) shell instance used by system().

    First difference is that HISTFILE variable gets lost. We need it to be recreated. I guess.

    Second difference is here:
    Code:
    -SHELLOPTS=braceexpand:hashall:interactive-comments
    +SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
    Amongst others, "history" option is missing from the SHELLOPTS variable. We need it to be set.

    I didn't investigate further, just worked around it in a straight way:

    Code:
      system("HISTFILE=~/.bash_history; set -o history; HISTTIMEFORMAT='%F %T ' history");
    Voila : )

  11. #10
    Just Joined!
    Join Date
    Sep 2010
    Posts
    14
    Quote Originally Posted by saulius2 View Post
    I've run shell builtin called "set" and it revealed essential differences between the shell I work and the (probably temporary) shell instance used by system().

    First difference is that HISTFILE variable gets lost. We need it to be recreated. I guess.

    Second difference is here:
    Code:
    -SHELLOPTS=braceexpand:hashall:interactive-comments
    +SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
    Amongst others, "history" option is missing from the SHELLOPTS variable. We need it to be set.

    I didn't investigate further, just worked around it in a straight way:

    Code:
      system("HISTFILE=~/.bash_history; set -o history; HISTTIMEFORMAT='%F %T ' history");
    Voila : )
    but i gives me the current time for all the past commands.
    i need the actual time of occurance of commands.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •