l i n u x - u s e r s - g r o u p - o f - d a v i s
L U G O D
 
Next Meeting:
October 20: Web Application Hacking: How to Make and Break Security on the Web
Next Installfest:
TBD
Latest News:
Oct. 10: LUGOD Installfests coming again soon
Page last updated:
2003 Jun 24 11:43

The following is an archive of a post made to our 'vox-tech mailing list' by one of its subscribers.

Report this post as spam:

(Enter your email address)
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



LinkedIn
LUGOD Group on LinkedIn
Sign up for LUGOD event announcements
Your email address:
facebook
LUGOD Group on Facebook
'Like' LUGOD on Facebook:

Hosting provided by:
Sunset Systems
Sunset Systems offers preconfigured Linux systems, remote system administration and custom software development.

LUGOD: Linux Users' Group of Davis
PO Box 2082, Davis, CA 95617
Contact Us

LUGOD is a 501(c)7 non-profit organization
based in Davis, California
and serving the Sacramento area.
"Linux" is a trademark of Linus Torvalds.

Sponsored in part by:
Sunset Systems
Who graciously hosts our website & mailing lists!