in reply to Re: Differentiating STDIN from arguments when using -n ?
in thread Differentiating STDIN from arguments when using -n ?

Thanks a lot. Indeed, with the BEGIN block, it now works.

In case someone is curious, I needed it in a shell script for a convoluted situation: to be able to un-mount a disk cleanly, I needed a smart sort on the mounted partitions of the disk.

# $dest was set to "sdb" through a shell argument mounted_disks=$(mount | perl -nae 'BEGIN{$d=shift}; push @m, $F[2] if +m{/dev/$d}; END{print join(" ", sort {length($b)<=>length($a)} @m)}' +$dest) umount -l $mounted_disks

Thanks to Perl and the Perl Monks, it works.

Replies are listed 'Best First'.
Re^3: Differentiating STDIN from arguments when using -n ?
by jwkrahn (Abbot) on Sep 15, 2007 at 19:50 UTC

    You don't really need a loop for that, you could do it like this:

    mounted_disks=$(mount | perl -le'$d = shift; print join " ", sort { le +ngth( $b ) <=> length( $a ) } map m{/dev/$d} ? (split)[2] : (), <>' $ +dest)

    Or even move the mount command into the perl code:

    mounted_disks=$(perl -le'$d = shift; print join " ", sort { length( $b + ) <=> length( $a ) } map m{/dev/$d} ? (split)[2] : (), qx(mount)' $d +est)
      Cool alternative, even if it took me a moment to understand... Thanks.