Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: Is it possible to determine last executed command line?

by tcf03 (Deacon)
on Dec 03, 2007 at 18:50 UTC ( #654625=note: print w/replies, xml ) Need Help??

in reply to Is it possible to determine last executed command line?

This may be a bit overkill, and definitely defines additional variables, but you could use a hash to store the command and any errors.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Digest::MD5; use IPC::Run qw( run timeout ) ; my %syscalls; runsyscall('/usr/bin/ls'); runsyscall('/bin/ls'); runsyscall('vipw'); print Dumper %syscalls; sub runsyscall { my @cmd = @_; my $digest = digestit(@cmd); my ( $in, $out, $err ); $syscalls{$digest}{'cmd'} = qq|@cmd|; eval { run \@cmd, \$in, \$out, \$err, timeout 10 }; if ($@) { $syscalls{$digest}{'exit'} = $@; } chomp ($err) if $err; # chomp ($out) if $out; $syscalls{$digest}{'err'} = $err || undef; # $syscalls{$digest}{'out'} = $out || undef; } sub digestit { my @cmd = @_; my $sysdigest = Digest::MD5->new; $sysdigest->add(@cmd); return $sysdigest->hexdigest; }

"That which we persist in doing becomes easier, not that the task itself has become easier, but that our ability to perform it has improved."
  --Ralph Waldo Emerson

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://654625]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2022-05-21 00:08 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (76 votes). Check out past polls.