SPUG:Best One-Liners and Scripts for UNIX

SPUG-list-owner tim at consultix-inc.com
Wed Apr 16 21:28:18 CDT 2003


On Wed, Apr 16, 2003 at 07:10:16PM -0700, Richard Anderson wrote:
> Well, this example of a scriptlet scores zero on the cleverness scale, but
> it has been so useful to me that I'll run it up the flagpole.  When I'm
> writing code I often find it faster to test a Perl feature rather than
> reading the online or printed docs (which are sometimes misleading).  So I
> run my psh (Perl shell) scriptlet:
> 
> #! /usr/bin/perl
> while (<>) {
>     eval;
>     if ($@) { print $@ }
> }

I fool around with this sometimes myself, because I have trouble
remembering the crazy command names in the -d mode (X? x?)

Thanks for your contribution!

-Tim
> 
> and type in the commands that test the feature.  No terminating semicolons
> are needed on the commands and the if statement displays any Perl error
> messages.
> 
> Trivial, but one of my more heavily-used scriptlets.
> 
> Richard Anderson
> richard at richard-anderson.org
> www.richard-anderson.org
> ----- Original Message -----
> From: "Tim Maher" <tim at consultix-inc.com>
> To: <spug-list at pm.org>
> Sent: Wednesday, April 16, 2003 5:49 PM
> Subject: SPUG:Best One-Liners and Scripts for UNIX
> 
> 
> > SPUGsters,
> >
> > Seems like most people stayed home to work on their taxes last
> > night, judging from the turnout at our April SPUG meeting 8-{
> >
> > But I gave my talk anyway -- well, at least *part* of it.
> >
> > It took me the full two hours to cover "Perl as a Better
> > Grep, Sed, and Awk", so I didn't delve into the other
> > advertised topics, which were:
> >
> >    * 5 Perl One-liners All Unix/Linux Users Should Know
> >    * 3 Perl Scripts UNIX/Linux Users Shouldn't Live Without
> >    * How Perl's Looping Facilities Compare to the Shell's
> >
> > I've got my own ideas about what the "5" and the "3" are, but I'm
> > very interested to get input from you folks on your favorites!
> >
> > So please give some thought as to what Perl one-liners or small
> > scripts you value for your UNIX/Linux work, and post them to the
> > list so we can discuss them.  If I like your submissions, with
> > your permission, I'll include them in my book, and you'll get
> > "your foot-noted". 8-}
> >
> > I'll start off the discussion with one of each.
> >
> > In the one-liner category, a command to convert MEMO.TXT (transferred
> > from some evil OS) to memo.txt:
> >
> > echo MEMO.TXT | perl -wlne 'rename $_, "\L$_\E"'
> > (It's processing input because arg-handling takes more typing.)
> >
> > ls *[A-Z]* | perl -wlne 'rename $_, "\L$_\E"'
> > (to rename all files in directory)
> >
> > Here's a *deluxe version* with error-checking:
> > perl -wlne 'rename $_, "\L$_\E" or
> > warn "rename failed for $_, $!\n"'
> >
> >     (Surprisingly, there's no warning for a missing file,
> >     but there's a missing file warning for a permission error
> >     -- at least on Linux!)
> >
> > In the script category, I offer one that sorts filenames by modification
> > time, which is something
> > find . -name 'x' | xargs ls -rdlt
> > can't be trusted to do, because of the possibility of separate sorts for
> > different argument sets with lots of filenames.
> >
> > I'll post the code samples from last night's talk on my web site soon,
> > and announce that to the list when it happens.
> >
> > Here's the script UNIX SAs shouldn't live without:
> >
> > #! /usr/local/bin/perl -w
> > # sort_files_by_mtime
> > # Tim Maher, tim at teachmeperl.com
> > #
> > # Sorts files by mod times, using Schwartzian transform,
> > # and prints filename and mod-time.
> > # Gives correct answers for large collections of names,
> > # where "find . -print | xargs ls -rdtl" cannot
> >
> > # NOTE: Use find or locate to provide input, or ls -d dir/*,
> > # but *not* simply "ls dir" (dir won't be present in pathnanme,
> > # which will trigger "invalid file" error
> >
> > # Sample invocations:
> > #   locate '*.c' | sort_files_by_mtime
> > #   find /local -name 'somescript' | sort_files_by_mtime
> > #   sort_files_by_mtime < /tmp/filelist
> >
> > $USAGE="Usage: $0 < file_list OR find ... | $0\n" ;
> > @ARGV  and  die "$USAGE" ;
> > @files=<STDIN>  or  exit 0 ;    # no input is okay
> > chomp @files ;
> >
> > $max=0 ;    # suppress warning
> > for ($_=0; $_ < @files; $_++) {
> >     if ($_ ne ""  and  -e $files[$_]) {
> >         length $files[$_] > $max  and
> >             $max = length $files[$_]
> >     }
> >     else {
> >         die "$0: Invalid filename, '$files[$_]'\n" ;
> >         delete $files[$_] ;
> >     }
> > }
> > @files  or  exit 0 ;
> >
> > map { printf "%${max}s  %s\n", $_->[0], (scalar localtime $_->[1]) }
> >     sort { $a->[1] <=> $b ->[1] }   # compare m-timestamps
> >         map { [ $_ , (stat $_)[9] ]  } @files ;
> >
> > -Tim
> > *------------------------------------------------------------*
> > |  Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX  |
> > |  CEO, JAWCAR ("Just Another White Camel Award Recipient")  |
> > |  tim at Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com   |
> > *+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
> > |  Watch for my  Book: "Minimal Perl for Shell Programmers"  |
> > *------------------------------------------------------------*
> > _____________________________________________________________
> > Seattle Perl Users Group Mailing List
> > POST TO: spug-list at mail.pm.org
> > ACCOUNT CONFIG: http://mail.pm.org/mailman/listinfo/spug-list
> > MEETINGS: 3rd Tuesdays, U-District, Seattle WA
> > WEB PAGE: www.seattleperl.org
> >
> >

-- 

-Tim
*------------------------------------------------------------*
|  Tim Maher (206) 781-UNIX  (866) DOC-PERL  (866) DOC-UNIX  |
|  CEO, JAWCAR ("Just Another White Camel Award Recipient")  |
|  tim at Consultix-Inc.Com  TeachMeUnix.Com  TeachMePerl.Com   |
*+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-*
|  Watch for my  Book: "Minimal Perl for Shell Programmers"  |
*------------------------------------------------------------*



More information about the spug-list mailing list