Extract history from Korn shell
I am not happy about the history file in binary format of the Korn shell.
I like to "collect" some of my command lines, many of them actually, and for a long time. I'm talking about years. That doesn't seem easy in Korn because the history file is not plain text so I can't edit it, and a lot of junk is piling up in it. By "junk" I mean lines that I don'twant to keep, like 'cat' or 'man'. So I added these lines to my .profile: fc -ln 1 9999 >> ~/khistory.txt source ~/loghistory.sh > ~/khistory.txt loghistory.sh contains a handful of sed and sort commands that gets rid of a lot of the junk. But apparently it is forbidden to run fc in the .profile file. I can't login whenever I do, the shell exits right away with signal 11. So I removed that 'fc -l' line from my .profile file and added it to the loghistory.sh script, but the shell still crashes. I also tried this line in my .profile: strings ~/.sh_history >> ~/khistory.txt source ~/loghistory.sh That doesn't crash, but the output is printed with an additional, random character in the beginning of many lines. I can run 'fc -l' on the command line, but that's no good. I need to automate that. But how? How can I extract my ksh history as plain text? TIA |
Quote:
Set your HISTSIZE environment variable to something reasonable like, oh, I dunno, 1000 so you'll have enough to deal with and use history -500 or something to see the last 500 commands. Here's histfile.c to get you started Code:
|
The 'history' command doesn't work:
Code:
/usr/bin/ksh: /home/luc/loghistory.sh[4]: fc: no history (yet) |
what about script?
Code:
NAME |
Yeah, it saves WAY too much.
|
what ksh you using?
what platform? the proper one? I use pdksh and my history is in plaintext what you could do is specify an EXIT trap in your profile. Code:
trap command EXIT this will invoke each time you leave a shell. |
Actually, you don't have to know anything about C -- save the source code file as histfile.c, then
Code:
make histfile Code:
cc -o histfile histfile.c Code:
histfile /home/luc/.sh_history > whatever_file_you_want I wonder, though, if you're running Korn Shell as your login shell, why the history command does not work? It's not going to work if your login shell is BASH (because that's not Korn Shell and there won't be any history entries) perhaps... Korn Shell records every command in, by default, ~/.sh_history and you can read that with the Korn Shell built-in history command, or fc or the histfile program listed above. If you have a different history file defined (the HSITFILE environment variable), use that file name instead. Anyway, best of luck with it. |
ksh on solaris also has text history file.
????? |
@bigearsbilly, I am using mksh on Kubuntu Linux. I had too much trouble with key bindings with both ksh93 and pdksh. The key bindings are a lot better with mksh. But they all had binary history files. I checked. I deleted them and let korn recreate them in each case.
Your tip involving 'trap' isn't very clear, I'll have to read the friendly manual to get acquainted with the 'trap' command. @tronayne Wow, that's a useful tool. Thanks! But the output is given entirely in one single line. Can you modify it so it will break the lines? Or am I doing something wrong? Korn is indeed my login shell and the history command is aliased to 'fc -l'. I think it's hard wired, I don't think I can change it. It works as advertised, but only interactively. It won't work in the .profile (.kshrc in my case) or an external script. |
Quote:
Code:
unalias history try this: Code:
$ ksh |
@tronayne I just realized your program also prepends an extra character to some lines, just like the output of 'strings'. Except 'strings' outputs multiple lines, so it's a little better than your program. :(
@bigearsbilly I can unalias history, but then I try to use it and just get this: /usr/bin/ksh: /home/luc/.kshrc[32]: history: not found That figures. There is no 'history' executable in the file system, it's usually a Bash builtin, which Korn does not have. Well, it does, but it's a different resource. |
Actually, the Korn Shell built-in is hist; from the ksh manual page (along with others)
Code:
The following aliases are compiled into the shell but can be unset or redefined: Code:
alias The hist built-in must be used as hist -l to get a listing of the shell history (otherwise it opens an ed-like editor). These are, of course, "real" Korn Shell rules and regulations and your mileage may vary significantly if you're using an emulator or non-Korn port. I'm interested: just what extra character(s) are prepended? Space character, tabs, something else? When I threw the program together I tested it against my own .sh_history file and got zero funky characters (all non alphanumeric or punctuation characters are ignored with the exception of tab, line feed and space, so I'm kind of wondering). |
I'm using mksh and it doesn't have 'hist'.
The extra characters are the same as the ones in 'strings': letters and numbers, sometimes a fairly common symbol, like = or @. |
All times are GMT -5. The time now is 02:27 PM. |