Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

$bash_history = '~/.bash_history'; unlink ($bash_history) || die "Error:$!\n"; system("touch $bash_history"); chmod (0700,$bash_history) || die "Error:$!\n";
is this safe to use? can it be optimized any further? I know I can just do a:
rm .bash_history ; ln -s /dev/null .bash_history
but I don't want to :)

Replies are listed 'Best First'.
Re: crontab and perl script using system
by Zaxo (Archbishop) on Jul 19, 2001 at 13:38 UTC

    That looks ok, but here is a straight perl approach:

    my $bash_history = '/home/Anonymous/.bash_history'; open BH, "> $bash_history" and close(BH) or die "Could not empty bash_ +history: $!";

    The history file is opened to write, not append, so its contents are lost but its permissions unchanged. perl 'and' binds a little tighter than 'or', so if either open or close fails, die is seen next.

    After Compline,
    Zaxo

Re: crontab and perl script using system
by MZSanford (Curate) on Jul 19, 2001 at 13:30 UTC
    Hello, I am a pure-perl kinda guy, and wouold probabley use :

    #!/usr/bin/perl -w use strict; my $FILE = "$ENV{HOME}/.bash_history"; truncate($FILE,0) || die "Could not empty '$FILE' : $!\n"; ## if truncate is not supported, use # open(A,"> $FILE") || die "Could not blank $FILE\n"; # close(A);

    OH, a sarcasm detector, that’s really useful
(ar0n) Re: crontab and perl script using system
by ar0n (Priest) on Jul 19, 2001 at 13:51 UTC
    I don't like golf that much, but...
    use IO::File; IO::File->new(">$ENV{HOME}/.bash_history") or die "Can't empty history +: $!\n";
    Since new returns a reference to a filehandle, but because that reference isn't saved, the refcount drops to zero and the file is closed right after opening.

    IO::File is included with the distribution, btw

    ar0n ]

Re: crontab and perl script using system
by perigeeV (Hermit) on Jul 19, 2001 at 16:24 UTC
    Some great Perlish ways have already been given above, but at the risk of being a complete heretic:

    #!/bin/sh cat /dev/null > ~/.bash_history

    I'll go say a dozen Hail Larry's for my transgretion.

      It's never wrong to use the best tool for the job. Remeber, one of the Virtues of being a Programmer is Laziness. It might sound like Heresy, but Perl is not always the best tool for the job. It's often the best, by orders of magnitude, but not always.
      Always keep a song in your heart.
      It's like karaoke for the voices in your head.
Re: crontab and perl script using system
by kschwab (Vicar) on Jul 20, 2001 at 05:09 UTC
    Alternatively, you could set the HISTFILE environment variable to /dev/null in your .profile or .bash-profile. Seems like less work :)