[RESOLVED] Re: [vox-tech] Shell Scripting Question: Getting a directory name from a 'find' result

Richard Crawford vox-tech@lists.lugod.org
Tue, 24 Jun 2003 11:01:30 -0700 (PDT)


Sorry for breaking the thread; I did not have the original message in my
in-box and I wanted to send the final solution I created (is there a way
in SquirrelMail to restore the thread in this situation?).

Once I got out of the mindset of having to use a bash shell script for the
task (thanks to Mike Simons), I came up with this script that does the
trick (specific IP address and directory structure stuff changed to
protect the innocent):

#!/usr/local/bin/perl -w

use strict;
use File::Find;

# $start_dir is the directory passed in the command line.
my $start_dir = shift or die "Usage: $0 <start_dir>\n";

# make sure $start_dir is a directory, and not a file
unless (-d $start_dir) {
  die "Start directory '$start_dir' is not a directory.\n";
}

# traverse the directory tree
find(\&process, $start_dir);

# change the files in the directory tree
sub process {

        # Only alter files called wwwboard.html
        return unless /\wwwboard.html$/;

  # get the current directory name
  my $dir = $File::Find::dir;

        # create an absolute path to wwwboard.pl
  $dir =~ s/\/part\/of\/directory\/to\/delete\//g;
  my $URL="http://xxx.xxx.xxx.xxx/".$dir."/wwwboard.pl";

  # Open the file for reading
  unless (open IN, $_) {
    warn "Can't open file $_ for reading: $!\n";
  }

        # Create an output file
  unless (open OUT, "> fixedfile") {
    warn "Can't open file 'new' for writing: $!\n";
  }

        # replace 'action="wwwboard.pl"' with 'action="$URL"'
  while (<IN>) {
    s/action="wwwboard\.pl"/action="$URL"/g;
    (print OUT $_) or die "Can't write to new file";
  }

        # Close the files
        close (IN);
        close (OUT);

        # make a backup, in case we screwed up
        rename ("wwwboard.html", "wwwboard.html.old");
        rename ("fixedfile", "wwwboard.html");

        # wwwboard.html must have 777 permissions for the wwwboard
software to work
  chmod 0777, 'wwwboard.html';
}

===========

Thanks again to everyone who responded with suggestions.

-- 
Sliante,
Richard S. Crawford

http://www.mossroot.com
AIM: Buffalo2K ICQ: 11646404 Y!: rscrawford
MSN: underpope@hotmail.com

"It is only with the heart that we see rightly; what is essential is
invisible to the eye." --Antoine de Saint Exupéry