[mdlug] OT: perl help (easy)

Daniel Hedlund daniel at digitree.org
Wed May 9 00:16:08 EDT 2007


The biggest problem with your code is that your copy command is
incorrect.  You're saying that you want to copy a single file to
multiple directories, but what you have in code is copying a single
file to a single directory multiple times, overwriting itself again
and again.  I assume that you were probably looking something along
the lines of:
system ("cp /tmp/foo.c /PATH/data/$_/"); ?

Of course, a directory needs to exist under /PATH/data with each of
the usernames listed.  That is unless you want to create those
directories as needed:
system ("mkdir -p /PATH/data/$_");
system ("chown $_ /PATH/data/$_");

You could simplify all this down considerably by using a single
command called "install" (run "man install" on the command line).
This would replace all of your system calls with a single one:
system ("install -D -o $_ /tmp/foo.c /PATH/data/$_/foo.c");

This would create all the directories, copy the file and set
permissions accordingly.  This could all be accomplished with an
incredibly simple BASH script containing:

for i in `cat /tmp/idlist`; do
    install -D -o $i /tmp/foo.c /PATH/data/$i/foo.c
done

Note.  If there are any spaces in any of the directory names,
usernames, etc, they will cause problems in both BASH and Perl's
system() function unless properly escaped.

Cheers,

Daniel Hedlund
daniel at digitree.org


On 5/8/07, dean <mdlug at sbcglobal.net> wrote:
> Hello, I have a perl question, and I'm thinking some people will say,
> this is very easy, figure it out yourself.    But if someone will help,
> it will save me some time, and time is kind of critical.
>
> I want to use perl copy the same file (/tmp/foo.c) to
> multiple directories that all have the format:
>
> /PATH/data.
>
> I have a file, idlist, that contains all the usernames, one per line each:
>
> usernameA
> usernameB
> .
> .
> .
> usernameZZ
>
> This is how I tried to do it, in perl:
>
> open (SAP,"/tmp/idlist");
> while (<SAP>)  {
>
> chomp;
>
> system ("cp /tmp/foo.c /PATH/data");
> system ("chown $_ /PATH/data/foo.c");
> }
>
> But this is not working, for reasons that I'm sure will be obvious to many.
> Would anyone be so kind as to provide a corrected perl program that will do
> this?
>
>  Thanks.  I say OT because it's Solaris.
>
>
>
>
>
>
>
>
>
>
>
> --
> This message has been scanned for viruses and
> dangerous content by MailScanner, and is
> believed to be clean.
>
>
>
>
> _______________________________________________
> mdlug mailing list
> mdlug at mdlug.org
> http://mdlug.org/mailman/listinfo/mdlug
>



More information about the mdlug mailing list