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

Mike Simons vox-tech@lists.lugod.org
Tue, 24 Jun 2003 14:24:17 -0400


--kD0q5DIPXzY/lIlp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On Tue, Jun 24, 2003 at 11:01:30AM -0700, Richard Crawford wrote:
> #!/usr/local/bin/perl -w
>=20
> use strict;

great!

> # traverse the directory tree
> find(\&process, $start_dir);
>=20
> # change the files in the directory tree
> sub process {
>=20
>         # 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 =3D $File::Find::dir;
>=20
>         # create an absolute path to wwwboard.pl
>   $dir =3D~ s/\/part\/of\/directory\/to\/delete\//g;
>   my $URL=3D"http://xxx.xxx.xxx.xxx/".$dir."/wwwboard.pl";

$dir =3D~ 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=3D"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";
>   }
>=20
>         # 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=3D"wwwboard.pl"' with 'action=3D"$URL"'
>   while (<IN>) {
>     s/action=3D"wwwboard\.pl"/action=3D"$URL"/g;
>     (print OUT $_) or die "Can't write to new file";
>   }
>=20
>         # 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=20
files.

--=20
GPG key: http://simons-clan.com/~msimons/gpg/msimons.asc
Fingerprint: 524D A726 77CB 62C9 4D56  8109 E10C 249F B7FA ACBE

--kD0q5DIPXzY/lIlp
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE++JdR4Qwkn7f6rL4RAgS2AJ91U1mgijxZBEK1tY//ZvtiqkX18gCgoVx2
25X0+nk4DgH6O4v0y1g5XOU=
=nbbG
-----END PGP SIGNATURE-----

--kD0q5DIPXzY/lIlp--