I smell the parsing of smbstatus -L, looking for file locks on Samba-mounted drives...

The use of split isn't quite enough, should you (like me) happen to have users who create file names with spaces. You have to do it in two parts, working forwards and backwards to isolate the file name. I have some ugly code to do this.

Warning: ugly code ahead!

This is not at all bullet-proof, but feel free to adapt the nuggets to do something better.

#! /usr/bin/perl -w use strict; open IN, '/usr/local/bin/smbstatus -L 2>/dev/null |' or die "Cannot open input pipe from smbstatus: $!\n"; while( <IN> ) { chomp; last if /^-*$/; } while( <IN> ) { chomp; my( $pid, $mode, $rw, $oplock, $rest ) = split(' ', $_, 5); my( $name, $dayname, $mon, $day, $timestamp, $year ) = ($rest =~ /(.*?)\s+(\S{3})\s+(\S{3})\s+(\d+)\s+(\d+:\d+:\d+)\s ++(\d+)$/); last unless defined $pid; print "[$pid] [$name]\n"; }

I hacked this up ages ago. I guess I should dust it off and make it a little cleaner. When I get a round tuit.


--
g r i n d e r

In reply to Re: Splitting each word in a string by grinder
in thread Splitting each word in a string by chaskins

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.