Re: [RESOLVED] Re: [vox-tech] Shell Scripting Question: Getting adirectory name from a 'find' result
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [RESOLVED] Re: [vox-tech] Shell Scripting Question: Getting adirectory name from a 'find' result
On Tue, Jun 24, 2003 at 11:01:30AM -0700, Richard Crawford wrote:
> #!/usr/local/bin/perl -w
>
> use strict;
great!
> # 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$/;
the '\' should not be here... it will match things like
'hwwboard.html', or '0wwboard.html'. If that is what you want change
the comment.
> # 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";
$dir =~ s#/part/of/directory/to/delete##g;
You can use any character you want to control start/stop marks for
's///' and 'm//' operators in perl. If you are going to be working with
strings that have a slash don't use them as the operator markers. ;)
This will make your life much more enjoyable later.
my $URL="http://xxx.xxx.xxx.xxx/$dir/wwwboard.pl";
No reason to put $dir outside the double quoted string... it works
fine inside and is more readable.
> # 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";
> }
For both of these error cases you should return after doing the warn,
maybe even "die" instead of warn. If you don't the rest of the function
will continue to happen and that is not what you want.
Also the second "warn" above should complain about opening 'fixedfile'
instead of 'new'.
> # 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);
good.
> # make a backup, in case we screwed up
> rename ("wwwboard.html", "wwwboard.html.old");
> rename ("fixedfile", "wwwboard.html");
You may want to consider:
link ("wwwboard.html", "wwwboard.html.old");
rename ("fixedfile", "wwwboard.html");
This will prevent there ever being a moment in time where the
winboard.html file does not exist.
> # wwwboard.html must have 777 permissions for the wwwboard
> software to work
> chmod 0777, 'wwwboard.html';
> }
It seems odd that the software would require the .html file to be
executable. Are you sure it's not '666' that the software requires?
If this is the case you can "umask 0000;" at the start of the
program, then you won't need to run this chmod operation for any
files.
--
GPG key: http://simons-clan.com/~msimons/gpg/msimons.asc
Fingerprint: 524D A726 77CB 62C9 4D56 8109 E10C 249F B7FA ACBE
Attachment:
pgp00028.pgp
Description: PGP signature
|