CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
trying to properly create directory

 



regex2012
User

Jan 27, 2017, 1:36 PM

Post #1 of 4 (1477 views)
trying to properly create directory Can't Post

I've been trying to create a directory like this:
my $strHostName = `hostname`;


Code
my $dir="/prj/inventory/reports/accounting/$strHostName"; 
mkdir $newdir, 0777;


This works, but the only problem is I cannot cd to it like this:
/prj/inventory/reports/accounting/nameofhost.

I can cd to /prj/inventory/reports/accounting/'hostname'/


why?
I need to cd the usual way /prj/inventory/reports/accounting/hostname without the 'hostname'/ single quotes.

how to get this to work?


FishMonger
Veteran / Moderator

Jan 27, 2017, 2:55 PM

Post #2 of 4 (1475 views)
Re: [regex2012] trying to properly create directory [In reply to] Can't Post

The value of $strHostName is not exactly what you think. It ends with a \n line terminator which needs to be removed before you create the directory.

Also, the mkdir command will not create a directory tree. It will only create the last segment of the path if the path preceding it actually exists. If you need to create the full directory tree, then you should look at using the make_path function from the File::Path module.


regex2012
User

Jan 30, 2017, 8:33 AM

Post #3 of 4 (1448 views)
Re: [regex2012] trying to properly create directory [In reply to] Can't Post

 
Ok I was able to do this by these statements:


Code
chomp( my $strHostname = qx{ hostname }); 
print $strHostname;

my $newdir="/prj/inventory/reports/accounting/$strHostname";
mkdir $newdir, 0777;


open (my $file, '>', '/prj/inventory/reports/accounting/$strHostname/listofaccounts.txt') or die "Could not open file: $!";
my $bpjoutput = qx(/bin/reportingmod -all_columns);
die "$!" if $?;
print $file $bpjoutput;


The directory is created just fine, but the listofaccounts.txt never gets created and the error "no directory" comes up, even though I can cd to the directory "benny" which is the hostname that $strHostname would contain.

It does work if I use the actual name of the host, for example,

Code
open (my $file, '>', '/prj/inventory/reports/accounting/benny/listofaccounts.txt')



However, I can't get a file created in there if I use the $strHostname variable.
how to fix this?


(This post was edited by regex2012 on Jan 30, 2017, 8:35 AM)


regex2012
User

Jan 30, 2017, 8:38 AM

Post #4 of 4 (1445 views)
Re: [regex2012] trying to properly create directory [In reply to] Can't Post

Never mind and thanks for the help! The problem was I had single quotes and not double, now it works!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives