Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdir failing to create directory in perl

Tags:

perl

i think the script previously given was confusing ,this is the script i want to run now, which is not creating directory, please help me in this.

I don't mynd if the code is idiotic , as I am just a beginner in perl. Could you suggest me the right approach for this script, and if I have any errors?

The output_folder1.txt contains 10010, and output_folder.txt_2 contains 30001.

I am getting output file cannot be created.

#!/usr/local/bin/perl -w
use strict;


print "Content-type:text/html\n\n";

my(@folder_name,$temp1,$temp2);

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(@folder_name,<TWO>);
close TWO;


print $folder_name[0],"\n",$folder_name[1],"\n";

$temp1 = pop(@folder_name);
$temp2 = pop(@folder_name);



if($temp1 < 30050)
{

    mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1",0777 or die "File cannot be created";
}
like image 345
user1077347 Avatar asked Dec 12 '22 07:12

user1077347


2 Answers

You need to add $! to your die string (die "File cannot be created: $!"). That'll give you the reason why. $! is the error message from the operating system. See "Error Variables" in perlvar.

It seems quite possible you're trying to mkdir two directories at once (…/$temp2 and …/$temp2/$temp1). You need two mkdir calls for that, or use File::Path's make_path.

like image 58
derobert Avatar answered Feb 10 '23 16:02

derobert


You should first check if $temp2 directory exists:

unless ( -d "/var/www/html/piRNA_html/UNAFold/output/$temp2" ) {
    mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2",0777 or die $!;
}
mkdir "/var/www/html/piRNA_html/UNAFold/output/$temp2/$temp1",0777 or die $!;
like image 23
gangabass Avatar answered Feb 10 '23 14:02

gangabass