开发者

Perl scripts that has the same function as Unix Command "history"?

Is it possible to write a Perl script to grab the history of Unix commands? Which means write a Perl scripts that has the same function as Unix Command "history"?

I tried to use tcsh history but the history does not contain latest Unix commands. The latest will only be available after I close the current xterm. Is there a way to solve it without closing the xterm?

I saw something in other post which is PROMPT_COMMAN开发者_JS百科D='history -a', how to use it? T.T

Thanks.


You can control this behavior by editing your .tcshrc file found in your home directory.

If you alias precmd, that will be run before each prompt is shown. The command you want is history -S which will force a save of your current history. Assuming that you're running multiple terminals at once, you'll also want to set savehist so that it merges all of the histories rather than just overwriting. Without the merge option, the history file would be overwritten with your current terminal's history, but as soon as you went to another window, the history would be overwritten with that terminal's history.

So, assuming you want to keep 500 lines of history (merged), here are the two lines you need to add into your ~/.tcshrc file. Note, unless you've changed histfile, your history will be found in ~/.history:

set savehist = (500 "merge")
alias precmd "history -S"

In closing, I'd strongly suggest (in agreement with @Mimisbrunnr) that you look at using either bash or zsh as they've incorporated the best parts of tcsh along with the power of the Bourne shell (sh) leaving out all of the weaknesses of csh.

Oh... and if you want a perl script to get the history, use something like:

#!/usr/bin/perl
use strict;
use warnings;

open my $hist_fh, '<', "$ENV{HOME}/.history"  or die "Cannot open history file: $!\n";

my @history;
while (<$hist_fh>) {
    chomp;
    next if /^#/;  # Skip timestamps
    push @history, $_;
}
close $hist_fh;

print "Now I have my history in an array, the latest thing I did was: $history[-1]\n"
    if @history > 0;  # show latest command if we were able to read the history file


Have you seen

 Term::ReadLine::Gnu

http://search.cpan.org/search?mode=all&query=term+readlin+gnu

on cpan?

SO has a

 readline

tag for it and the other implementations.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜