Reputation: 587
I'm having trouble starting this code, I really have no idea where to start. I'm trying to understand what to do. Pretty much what I have to do is once i execute the file i should be able to just type in "history" and it should bring up all my history of commands used. How would I code this file to allow me to do that? Any hints or clues would be appreciated!
Upvotes: 0
Views: 1631
Reputation: 753695
The basics are relatively straight-forward. You need to design a suitable on-disk format for the history, bearing in mind you probably don't want the file to grow indefinitely large (though that might be OK for a first version). And you'll need a set of functions to manipulate the file (open, close, write, search).
Each time you read a command (complete command — a pipeline, or a for
loop, or whatever), you also write to your history mechanism.
Your history mechanism should also allow you to retrieve previous commands, to list them, or to search them by content or by number.
Niceties you should consider include ensuring that the history file is closed when other commands are executed, and ensuring sub-shells don't write to the history (usually). You need to think about whether you can have multiple shells accessing the same history file; will your history persist across sessions? What happens if you have two terminal windows open at the same time?
The GNU readline library has hooks to make it possible to integrate with a history library. It also provides a history library.
Upvotes: 3