Solved csh history

talsamon

Daemon

Reaction score: 283
Messages: 1,835

Every time after I reboot the csh shell history is puzzled (Have this problem since I am back to FreeBSD with 8.X, find no reason and no solution). csh is login shell. Can I anything do to prevent this? This never happened with bash. It does not happen if I manually close the window-manager (fvwm-crystal) and log out on the terminal. But there must a way to reboot directly from fvwm-crystal, without crash the history, bash can do this.
 
Last edited by a moderator:

protocelt

Daemon

Reaction score: 413
Messages: 1,253

You need to set the history options for the shell. For example, in my user's .tcshrc file I have the following:
Code:
# set shell history
set history = 2048

set histfile = ~/.tcsh_history
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

I have no tcsh_history, only .history - but this does not matter. Thanks, I will test it.
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

I thought I have something formatted wrong. I deleted the post as I saw it is a standard footer. Thanks.
 

protocelt

Daemon

Reaction score: 413
Messages: 1,253

Oops, I forgot to add the merge line in my output above. Sorry about that. Try this:
Code:
# set shell history
set history = 2048
set savehist = (2048 merge)
set histfile = ~/.tcsh_history # This line is optional
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

Looks per default so:
Code:
...
if ($?prompt) then
  # An interactive shell -- set some stuff up
  set prompt = "%N@%m:%~ %# "
  set promptchars = "%#"

  set filec
  set history = 1000
  set savehist = (1000 merge)
  set autolist = ambiguous
  # Use history to aid expansion
  set autoexpand
  set autorehash
  set mail = (/var/mail/$USER)
  if ( $?tcsh ) then
  bindkey "^W" backward-delete-word
  bindkey -k up history-search-backward
  bindkey -k down history-search-forward
  endif

endif
...
 

protocelt

Daemon

Reaction score: 413
Messages: 1,253

Yes, that's root's default .cshrc. You can copy it over for your user to use or create a new file for the user.
 

protocelt

Daemon

Reaction score: 413
Messages: 1,253

Ok, I'm getting a bit confused here...

Both csh(1) and tcsh(1) are tcsh. They are the same shell. The only difference is when csh is used to invoke the shell, some options are turned off by default. In short, use tcsh and not csh to start the shell unless you have some reason to do otherwise.

It might help to see your entire .cshrc file.
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

After a few reboots, all seems ok. Seems it was the change to tcsh. Seems solved.
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

No, it not solved. History is puzzled again (now with tcsh).
 

getopt

Aspiring Daemon

Reaction score: 474
Messages: 750

Here is a Perl script that I use to clean ~/.history.
Code:
#!/usr/bin/env perl 
#===============================================================================
#
#         FILE: history_cleaner.pl
#
#        USAGE: ./history_cleaner.pl  
#
#  DESCRIPTION: Cleans th ~/.history file from some entries
#
# REQUIREMENTS: ~/.history
#        NOTES: clean the history file and load the new history from file
#               cannot be done within this script. It has to be done manually,
#               after running the script.
#       AUTHOR: Getopt 
#      VERSION: 1.1
#  LAST CHANGE: 30.08.2015
#
#===============================================================================

use v5.18;
use File::Copy;

my $historyfile = "$ENV{HOME}/.history";
my $historydebugfile = "$ENV{HOME}/history_debug";

my $debug = 0;
if ( $debug ) {
    if ( !-e $historydebugfile ) {      # Test if file does not exist
        copy($historyfile, $historydebugfile) or die "Copy failed: $!";
        say "DEBUG: $historyfile has been copied to $historydebugfile";
    }
    else { 
        say "DEBUG: Working on $historydebugfile";
    }
    $historyfile = $historydebugfile;
}

sub usage {
  say "Usage:  history_cleaner delete expr   deletes matching expression";  
  say "        history_cleaner auto          cleans not useful entries";
  say "        history_cleaner repair        cleans corrupted lines";
}

if ( @ARGV == () || @ARGV[0] !~ m/auto|rep|repair|del|delete/ ) {
  usage();
  exit;
}

# read the file in an array
open FILE, "<", $historyfile or die "Error opening $historyfile: $!\n";
    my @line = <FILE>;
close (FILE);
chomp(@line);

my $minlen = 6;     # Minimum length of command line
my @newline;

foreach my $i ( 0 .. $#line ) {
    if ( $line[$i] =~ s/^#\+//) {
        # $_ is now epoch (we might use it someday)  
        # $line[$i+1] has the command line

        # Delete corrupted lines
        if ( $ARGV[0] =~ m/rep|repair/ && $line[$i+1] =~ m/#\+/) {
            say "deleted: $line[$i+1]";
            next;
        }
        # Delete small lines
        elsif ( $ARGV[0] =~ m/auto/ && length($line[$i+1]) <= $minlen) {
            say "deleted: $line[$i+1]";
            next;
        }
        # Delete unneccesary or unwanted commands
        elsif ( $ARGV[0] =~ m/auto/ &&
                $line[$i+1] =~ m/^(pkg i|pkg d|dmesg|drill|which|chown|chmod|touch|tail|head|host|kill|diff|opie|echo|eval|find|wipe|set|setenv|cat|cal|man|cd|cp|dc|ls|ll|mv|rm|[0-9]|\~|\+|\.|\/|\$)/ ) {
            say "deleted: $line[$i+1]";
            next;
        }
        # Delete matched command lines
        elsif ( $ARGV[0] =~ m/del|delete/ && $line[$i+1] =~ m/$ARGV[1]/ ) {
            say "deleted: $line[$i+1]";
            next;
        }
        else {
            push @newline, "#+$line[$i]";
            push @newline, $line[$i+1];
        }
    }
}

system("mv $historyfile $historyfile~");
open FILE, ">", $historyfile or die "Error opening $historyfile: $!\n";
map {
    say FILE $_;
} @newline;
close (FILE);

say "To make the changes permanent, you need to run manually:";
say  "> history -c && history -L $historyfile\n";
 
OP
talsamon

talsamon

Daemon

Reaction score: 283
Messages: 1,835

That's a hit. Thank you. The script misses a right curly bracket on line 46. But seems to work.
 
Top