http://qs321.pair.com?node_id=941235

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

hii monks, nyc to meet u in this discussion and thanks in advance for helping in this

Problem: here with this script am unable to make a directory , am running it as cgi, please help me in this

#!/usr/local/bin/perl -w print "Content-type:text/html\n\n"; use File::Copy; use CGI::Carp qw(fatalsToBrowser); use CGI qw(:standard); #use strict; my($jobname,$sequence,$temperature,$sodium,$folding,$magnesium,$label, +$window); my(@folder_name,$temp1,$temp2); #my(@argument); $jobname = param('jobname'); $sequence = param('sequence'); $temperature = param('temperature'); $sodium = param('sodium'); $folding = param('folding'); $magnesium = param('magnesium'); $label = param('label'); $window = param('window'); open ONE,"<","/var/www/html/piRNA_html/UNAFold/output_folder_1.txt" || + die "Cannot open the file"; @folder_name=<ONE>; close ONE; open TWO,"<","/var/www/html/piRNA_html/UNAFold/output_folder_2.txt" || + die "Cannot open the file"; push(my @folder_name,<TWO>); close TWO; #print $folder_name[0],"\n", $folder_name[1]; $temp1 = pop(@folder_name); $temp2 = pop(@folder_name); if($temp1 < 30050) { mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1",0775 +; open DAT,">","/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1/$j +obname" || die "Can't open the file "; print DAT my $sequence; print ($temp2,$temp1,$jobname); close DAT; my $location = "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1" +; my $file1 = "unafold2.cgi"; copy $file1,$location || die "File cannot be copied."; system("chmod 777 $location/$file1"); $temp1++; open THREE,">","/var/www/html/piRNA_html/UNAFold/output_folder_2. +txt" || die "Can't open the file"; #print THREE $temp1; close THREE; } else { $temp1 = 30001; $temp2++; mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2",0777; mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1",0777 +; open DAT,">/var/www/html/piRNA_html/UNAFold/output/$temp2/$tem +p1/$jobname" || die "Can't open the file "; print DAT $sequence; close DAT; my $location = "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1" +; my $file1 = "unafold2.cgi"; copy $file1,$location || die "File cannot be copied."; system("chmod 777 $location/$file1"); $temp1++; open FOUR,">/var/www/html/piRNA_html/UNAFold/output_folder_2.txt" +|| die "Can't open the file"; print FOUR $temp1; close FOUR; open FIVE,">/var/www/html/piRNA_html/UNAFold/output_folder_1.txt" +|| die "Can't open the file"; print FIVE $temp2; close FIVE; }

Replies are listed 'Best First'.
Re: Problem in creating Directory
by davido (Cardinal) on Dec 02, 2011 at 06:15 UTC

    In line 32, 58, and 59, you're calling mkdir, but not checking the "return value" (which is to say, not checking for success or failure). It's very important to check for failure, and if failure occurs, inspect the contents of the special variable "$!" to see what went wrong. Otherwise we're all just going to be guessing.

    So, for example, on line 32, change to:

    mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1", 0775 or die "Failed to create directory ..../$temp2/$temp1: $!";

    Once you know what went wrong 3/4ths of the battle is won. We could wade through the code, ask a bunch of questions, and so on. But unless you're taking advantage of Perl's ability to provide clues you haven't done your own due diligence yet. :)


    Dave

      after adding $! , am getting the following error in my gi

      Software error: Cannot make directory /var/www/html/piRNA_html/UNAFold/output/10010 , errorwas:Permission denied at /var/www/html/piRNA_html/UNAFold/mkdirtest.cgi line 39.

      i couldnt understand , though 0777 permission given , y its giving error about permission, please reply me in this

        When you create a file or directory, you can define the permissions for the item you are going to create. These are the 0777 permissions given by You.

        As creating a new directory is nothing else but writing an entry into the directory above, you need write permissions for /var/www/html/piRNA_html/UNAFold/output.

        The user that needs those permissions is the cgi user, the script will run under.

        Here You can find some hints on CGI programming

        The error tells you "You do not have the permission to create the directory", while the permission you give means "after creating the directory, give it this permission". These two permissions are totally unrelated.
        You've reported at least two completely different errors in this thread, "file exists", and "permission denied". For "permission denied", do this:
        ls -ld /var/www/html/piRNA_html/UNAFold/output

        What are the permissions on the directory? Do you (or the user this script is running under) have write permission in the directory?

      Thanks for the reply dave, yes ur ryt i havent done proper deligence, becoz am new to perl , and my perl knowledge is not upto the mark , when i changed accordingly its giving following error Failed to createeate directory /var/www/html/piRNA_html/UNAFold/output//: File exists at /var/www/html/piRNA_html/UNAFold/mkdirtest.cgi line 39.

        Now go to your linux prompt and type the following:

        mkdir mytest mkdir mytest

        You'll get the following error message:

        mkdir: cannot create directory mytest: File exists

        You can go ahead and rmdir that mytest directory now, and then ask yourself why you're trying to create a directory that already exists.


        Dave

        A reply falls below the community's threshold of quality. You may see it by logging in.

        The error message just tells You that the directory $somepath/output exists. Your variables $temp1, $temp2 seem to have been empty (see the trailing slashes in the error message).

        Besides the push statement in line 27 declares a second array @folder_names.

        You should really uncomment the "use strict" like already said. This will help You find and correct errors like this one.

Re: problem in mkdir
by GrandFather (Saint) on Dec 02, 2011 at 06:12 UTC
      Thanks for the reply and sorry GrandFa its not the question of use strict, its by mistake commented,though i uncommented that , am unable to make a directory in the path /var/...../temp2, am not getting any errors , but in error log its giving print() on closed filehandle DAT, i find this error becoz its not creating directory
Re: problem in mkdir
by remiah (Hermit) on Dec 02, 2011 at 05:45 UTC
    What does it say if you print error message of mkdir?
    mkdir ("/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1",0775) o +r print "in mkdir:$!";

    And maybe, you had better put your question not in "PerlMonks Discussion" but "Questions"...

Re: problem in mkdir
by Plankton (Vicar) on Dec 02, 2011 at 07:02 UTC
    The problem is that you are trying to create a directory in a directory that does not exists yet. Take a look at this:
    $ cat test2.pl #!/usr/bin/perl -w my $d1 = "d1"; my $d2 = "d2"; mkdir "/tmp/$d1/$d2" or warn __LINE__ . " mkdir /tmp/$d1/$d2 failed:$! +\n"; mkdir "/tmp/$d1" or warn __LINE__ . " mkdir /tmp/$d1 failed:$!\n"; mkdir "/tmp/$d1/$d2" or warn __LINE__ . " mkdir /tmp/$d1/$d2 failed:$! +\n"; $ ./test2.pl 6 mkdir /tmp/d1/d2 failed:No such file or directory
    So my first mkdir command fails to make direcotry /tmp/d1/d2 because /tmp/d1 does not exists.
      thanks for the reply plankton, but i created the directory before in that path and then compiled it but it didnt works, thanks for understanding the script and replying

      after adding $! , am getting the following error in my gi

      Software error: Cannot make directory /var/www/html/piRNA_html/UNAFold/output/10010 , errorwas:Permission denied at /var/www/html/piRNA_html/UNAFold/mkdirtest.cgi line 39.

      i couldnt understand , though 0777 permission given , y its giving error about permission, please reply me in this

Re: problem in mkdir
by quester (Vicar) on Dec 02, 2011 at 09:36 UTC

    I can see a few things that seem odd:

    my(@folder_name,$temp1,$temp2); ... @folder_name=<ONE>; ... push(my @folder_name,<TWO>); ... $temp1 = pop(@folder_name); $temp2 = pop(@folder_name);

    If you check your web server logs, or if you check your script with "perl -c scriptname.pl" you will see a warning

    "my" variable @a masks earlier declaration in same scope

    The second push declares a second array @folder_name which hides the first array of that name; that makes $temp2 undefined later.

    Once that is fixed, if you read the definitions of the push and pop functions carefully, you will notice that $temp1 gets the contents of the last line in file TWO if file TWO is not empty, otherwise it gets the last line in file ONE. $temp2 gets the contents of the next to last line in file TWO if there if there are two lines in file TWO, the last line in file ONE if there is one line in file TWO, and the next to last line in file ONE if file TWO is empty. In the simple case where both files are one line long, $temp1 gets the file TWO contents and $temp2 get file ONE's. Somehow I doubt that is what you had intended.

    If you wanted a single line in $temp1 from file ONE, and wanted a single line in $temp2 from file TWO, you could remove the unnecessary array @folder_name and just:

    my(@folder_name,$temp1,$temp2); ... $temp1 = <ONE>; ... $temp2 = <TWO>;
    You should also check the value returned from mkdir, since it may indicate an error. The easiest way is with the "... or die" idiom:
    mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2",0777 or die "Cannot make directory /var/www/html/piRNA_html/UNAFold/outpu +t/$temp2, error was $!";
      thnks quester , i changed as u said, but its still not creating directory , in that
Re: Problem in creating Directory
by quester (Vicar) on Dec 02, 2011 at 09:26 UTC

    I can see a few things that seem odd:

    # use strict;

    Why would you want to not turn strict mode on? Are you not interested in what parts of your code are peculiar enough that the compiler has code to look for them?

    Next, if you check your script with "perl -c scriptname.pl" you will see a warning

    "my" variable @a masks earlier declaration in same scope

    The second push declares a second array @folder_name which hides the first array of that name; that makes $temp2 undefined later.

    Once that is fixed, if you read the definitions of the push and pop functions carefully, you will notice that $temp1 gets the contents of the last line in file TWO if file TWO is not empty, otherwise it gets the last line in file ONE. $temp2 gets the contents of the next to last line in file TWO if there if there are two lines in file TWO, the last line in file ONE if there is one line in file TWO, and the next to last line in file ONE if file TWO is empty. Somehow I doubt that is what you had intended.

    If you wanted a single line in $temp1 from file ONE, and wanted a single line in $temp2 from file TWO, you could remove the unnecessary array @folder_name and just:

    my(@folder_name,$temp1,$temp2); ... $temp1 = <ONE>; ... $temp2 = <TWO>;
    You should also check the value returned from mkdir, since it may indicate an error. The easiest way is with the "... or die" idiom:
    mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2",0777 or die "Cannot make directory /var/www/html/piRNA_html/UNAFold/outpu +t/$temp2, error was $!";