jlk has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone! I have been lurking around here for quite some time and have now decided that there is no time like the present to start learning perl. Anywho, I am working on a perl script that's only purpose at the moment, is to create a few directories and a couple of zero byte files, and change their permissions.

Below is the code that I have written so far:

#!/opt/bin/perl -w use strict; use Getopt::Long; my $clientname; my $fdgid; GetOptions ("clientname" => \$clientname, # client name for direc +tory "fdgid" => \$fdgid); # client Filedrive GID + number mkdir "/home/jlk/tmp/data/stuff/archive/$clientname",0002; mkdir "/home/jlk/tmp/data/stuff/archive/$clientname/tmp",0002; mkdir "/home/jlk/tmp/data/stuff/archive/$clientname/not_proc",0002; mkdir "/home/jlk/tmp/data/stuff/archive/$clientname/enc_arch_files",00 +02; mkdir "/home/jlk/tmp/data/stuff/archive/$clientname/enc_arch_files/inc +oming",0002; mkdir "/home/jlk/tmp/data/stuff/$fdgid/users/incoming",0002; mkdir "/home/jlk/tmp/data/stuff/$fdgid/users/incoming/$clientname",000 +2; mkdir "/home/jlk/tmp/data/stuff/$fdgid/users/outgoing",0002; mkdir "/home/jlk/tmp/data/stuff/$fdgid/users/outgoing/history",0022; system ("touch /home/jlk/tmp/data/stuff/archive/$clientname/$clientnam +e.log"); system ("touch /home/jlk/tmp/data/stuff/archive/$clientname/${clientna +me}_ack.log"); chmod 0111, '/home/jlk/tmp/data/stuff/archive/$clientname/$clientname. +log'; chmod 0111, '/home/jlk/tmp/data/stuff/archive/$clientname/${clientname +}_ack.log';

In the above code, the directories "/home/jlk/tmp/data/stuff/archive" and "/home/jlk/tmp/data/stuff/$fdgid/users" already exist. The permissions are set to 777(wide open). Also, I am running this on a Solaris 9 system running Perl v5.8.0.


When I run the script in its present state as shown above, I get the following output:

/home/jlk: ./ecgsetup.pl --clientname testing --fdgid G7778888
touch: /home/jlk/tmp/data/stuff/archive/1/1.log cannot stat
touch: /home/jlk/tmp/data/stuff/archive/1/1_ack.log cannot stat


Now, in running this script, the directory "/home/jlk/tmp/data/stuff/archive/testing" should have been created, but instead is called "/home/jlk/tmp/data/stuff/archive/1". The directory "1" has permissions d---------. Also, the directory "/home/jlk/tmp/data/stuff/users/G7778888" is not even created.

I would love to know the error of my ways, if someone could just point it out. Thank you much in advance for any and all assistance.


Regards,
jlk

Seasame street was brought to you today by the letters M, I and T and the number 3.1415926535897932384626433832795

Replies are listed 'Best First'.
Re: Need help with script to create directories
by BrowserUk (Patriarch) on Jun 20, 2004 at 04:58 UTC

    Try changing the GetOptions parameters to:

    GetOptions ("clientname=s" => \$clientname, # client name for dir +ectory "fdgid=s" => \$fdgid); # client Filedrive G +ID number

    See the docs for the explanation.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon

      Ok, did that and now the directory named "1" is called "testing", and all other results are the same. Also, the same errors were produced:

      touch: /home/jlk/tmp/data/ecftp/archive/testing/testing.log cannot stat
      touch: /home/jlk/tmp/data/ecftp/archive/testing/testing_ack.log cannot stat

      Any idea what they mean?

      Regards,
      jlk

      Seasame street was brought to you today by the letters M, I and T and the number 3.1415926535897932384626433832795

        Here is something you might want to check (this is from the solaris stat manpage which you may want to read):

        "You do not need any access rights to the file to get this information but you need search rights to all directories named in the path leading to the file." (italics mine for emphasis)

        The permissions are set to 777(wide open)

        This may be true of the parent directory, but is it true of all the directories in the path? Have the permissions changed on any of them?

        davidj
Re: Need help with script to create directories
by Anonymous Monk on Jun 20, 2004 at 13:22 UTC
    Hi,
    As someone else noted, you need to pass strings to your input arguments, so you should use "=s" in the GetOptions hash.
    Your second problem is that your first directory ("/home/jlk/tmp/data/stuff/archive/$clientname") is being created with no RWX permissions at all, even for the user. Hence no sub-directories or files can be created or of course stat'd (you can't stat what doesn't exist :) So change the mask to something more reasonable, or just leave the default (don't pass a second arg to mkdir). Finally, a work of advice- you're working too hard :-) To make a directory tree, use the mkpath function from the File::Path module, e.g.:
    use File::Path; my $base_dir="/home/jlk/tmp/data/stuff/archive/$clientname"; mkpath(["$base_dir/tmp", "$base_dir/not_proc", "$base_dir/enc_arch_files/incoming" ], 0, 0755);
    Does this help?
    Regards,
    Offer K.

      Its funny! I had asked someone else and they said that the permissions in the 'mkdir' command were the umask, not what you wanted them to be set to, but now, seeing how it has worked, it makes perfect sense.

      **a little later** Ok, I tried what was suggested and made the permissions in the mkdir commands 0775 to reflect the permissions I want and we have different results. The directories in the archive directory were created, but the permissions are not correct. The directory /home/jlk/tmp/data/stuff/archive/testing has permissions of 744 instead of 775. I am stumped as to what happened there. The directories inside of the testing directory were created and with the same permissions, 744, instead of 755. The log files were also created, but with permissions of 644 instead of 666. Am I doing something wrong with permissions?

      Regards,
      jlk

      Seasame street was brought to you today by the letters M, I and T and the number 3.1415926535897932384626433832795

        From perldoc -f mkdir:

        mkdir FILENAME,MASK
           Creates the directory specified by FILENAME, with permissions
           specified by MASK (as modified by "umask").
        

        This means that, even if you specify 0777 as MASK, the actual permissions of the directory will be 0777 & umask. You can set the umask with the function of the same name in Perl or in the shell.

        -- 
                dakkar - Mobilis in mobile
        

        Most of my code is tested...

        Perl is strongly typed, it just has very few types (Dan)