Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: chomping the output of Shell.pm

by kcott (Abbot)
on Sep 09, 2012 at 05:11 UTC ( #992557=note: print w/ replies, xml ) Need Help??


in reply to chomping the output of Shell.pm

G'day Rolf,

Update: I posted a solution but later found it didn't work with commands that output multiple lines (e.g. ls, ps, etc.). Here's a new solution that doesn't have that problem. The original also had a portability issue: this one doesn't. (The spoiler below contains the original solution.)

My .perldb contains:

$ cat ~/.perldb for (qw{pwd date ls ps}) { $DB::alias{$_} = 's{^($_.*)$}{chomp(my $out = qx{$1}); print {$DB::OUT} $out} +e'; }

Obviously, substitute qw{pwd date ls ps} with your qw/commands/. See perldebug - Debugger Customization for a description of %DB::alias.

Here's a test run:

$ perl -d -e 1 Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> pwd /Users/ken/tmp DB<2> date Sun 9 Sep 2012 18:12:43 EST DB<3> date "%Y-%m-%d" date: illegal time format usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS +]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] DB<4> date "+%Y-%m-%d" 2012-09-09 DB<5> ls -l total 30824 ... drwxr-xr-x 7 ken staff 238 14 Jun 20:23 zip_test DB<6> ps -ef UID PID PPID C STIME TTY TIME CMD ... 501 74528 74527 0 23Aug12 ttys007 0:00.04 -bash DB<7> q $

The typo (date "%Y-%m-%d") was accidental but I left it in just to show that the mistake was handled in a reasonable fashion.


My original, faulty solution - don't use this!

Here's a technique that seems to work. I've only done limited testing. It doesn't use Shell or chomp. It also assumes that the shell command echo -n suppresses the output of trailing newlines.

My .perldb contains a single line:

$ cat ~/.perldb $DB::alias{$_} = 's{^$_(.*)$}{p qx{$_$1 | xargs echo -n}}' for qw{pwd +date};

Obviously, substitute qw{pwd date} with your qw/commands/. See perldebug - Debugger Customization for a description of %DB::alias.

Here's a test run:

$ perl -d -e 1 Loading DB routines from perl5db.pl version 1.33 Editor support available. Enter h or `h h' for help, or `man perldebug' for more help. main::(-e:1): 1 DB<1> pwd /Users/ken/tmp DB<2> date Sun 9 Sep 2012 14:22:23 EST DB<3> date "%Y-%m-%d" date: illegal time format usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS +]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] DB<4> date "+%Y-%m-%d" 2012-09-09 DB<5> q

The typo (date "%Y-%m-%d") was accidental but I left it in just to show that the mistake was handled in a reasonable fashion.

-- Ken


Comment on Re: chomping the output of Shell.pm
Select or Download Code
Re^2: chomping the output of Shell.pm
by LanX (Canon) on Sep 09, 2012 at 21:28 UTC
    Hi Ken,

    thanks for your solution, but I wanna use those functions in any place not only at start of line.

    e.g.

    rm grep {/.pl$/} ls

    If you wanna implement (start of line) aliases, you should simply us "!!"

    DB<112> =du !!du DB<113> du /tmp 8 /tmp/pulse-lanx 4 /tmp/virtual-lanx.AqXwcf ... DB<114> $DB::alias{du} => "s\adu\a!!du\a"

    !! cmd Run cmd in a subprocess (reads from DB::IN, writes to DB:: +OUT) See 'O shellBang' too.

    Cheers Rolf

    ) Yes I could use the file-glob <*> instead, but this is only an example for chaining commands

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-07-04 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (60 votes), past polls