How can I programmatically access bash command history in C?

I hate the bash history command. He never finds the story elements that I want. Sometimes I can get what I want with a story | grep XXX ', but often either the story is too long (400+ hits! yay) or too short (no hits. Boo).

I thought to myself: why didn’t I write a small “history assistant” command that allows me to basically “star” specific elements of a story and make them always appear in my story, both with a global star list and with a special star list.

That sounds amazing. So I naively tried to read the story by doing this:

system("history > blah")
FILE *fp = fopen("blah", "r")

Oh, that didn't work. Ah, I understand that the system () command runs in its own context and cannot access the bash history. Drats. So, I will try to read ~ / .bash_history

ba baaa ~ No. This allowed us to get the history before the last call of either a) history -w or b) the call to exit the shell.

So the question is: How can I programmatically access the current bash history in C?

(NB: No, not that in ~ / .bash_history, the current story, the exact result that you see when you enter the "story" from the prompt, and also why do you ask C? Why not ... but really, because I already have a convenient ncurses shell that I was going to use to allow me to get fantastic autocomplete and subnet selection history on regular terminals ...)

: , - , , , " bash".

. , "" , , . , . . . ", ", ; .:)

+5
3

alias cmd="history | cmd"

.

0

history , bash .

, help history man bash a while.

( . /). , . .

, , , , .bash_history c =)

: , . .bashrc :

PROMPT_COMMAND='history -a;history -n'

export HISTCONTROL=ignoredups
export HISTTIMEFORMAT='%F %T '
export HISTSIZE=100500

shopt -s histappend
shopt -s cmdhist

export HISTIGNORE="unwanted_command:unwanted_mask*"

, .

+2

function cd {
    builtin cd "$@"
    if [ -f .cwdhist ]; then
        history -r .cwdhist
    fi
}

.

function histedit {
    history -a
    historyhelper $HOME/.bash_history $HOME/.histout
    history -c
    history -r $HOME/.histout
}

The team history -aadds the whole story to the story. The function then calls your story assistant. You can put all the emoticons in this program so that your favorite teams are in the history and add them if they are not, and then leave your output in .histout(or something else). The function then continues, clears and reloads the history. Perhaps this should avoid clearing the story. Or perhaps the function should be interrupted if historyhelper returns a non-zero exit status. All kinds of things you can do here.

+2
source

All Articles