http://qs321.pair.com?node_id=91639


in reply to Splitting each word in a string

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