Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am fresher and would like your help in creating a text file in a specified directory.
I have create a perl script, which create a text file, and it works well.
But i want this to create in a specified directory.
Can anyone please help me out with this.
THe scrip is:
Code:
#!/usr/bin/perl
use POSIX;
#Directory where the text file will be created.
$directory = '/space/data';
if (!(-d "$directory")) { #check if directory exists - returns true if doesn't exist.
printf "No directory\n", $? >> 8;
exit (1); #exit because directory doesn't exist.
}
my $filename=sprintf("%s_%s.TXT", "prefix", strftime("%y%M%d", localtime));
print "filename is $filename\n";
open(FILE, ">$filename") || die("Couldn't open file");
#print FILE "hello world\n";
print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);
I am not an expert in perl, but as far as I can see you have specified the path in $directory and the filename in $filename. But I can't see where you concatenate these variables. You only specify the filename in your open statement, but not the directory.
but also its not creating the .TXT file in the specified folder.
Well this would stand to reason ... let me answer you with a question, how would you create the same file using 'touch' in the
directory you have listed?
Well being a linux questions site I am unsure how it works on windows, although I am guessing the same.
Why don't you try concatenating your directory with the string in your sprintf and see how you go?
#!/usr/bin/perl
use POSIX;
#Directory where the text file will be created.
$directory = '/home/mstone';
if (!(-d "$directory")) { #check if directory exists - returns true if doesn't exist.
printf "No directory\n", $? >> 8;
exit (1); #exit because directory doesn't exist.
}
$filename="$directory/mstonetest.TXT";
print "filename is $filename\n";
open(FILE, ">$filename") || die("Couldn't open file");
#print FILE "hello world\n";
print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);
It's on Linux, so you might need to make modifications for Windows.
Windows was just an example. I am not working on windows. I am creating this perl script on AIX box.
I tried to concatenate the directory path and the file name, and i get this error message;
Operator or semicolon missing before %s_ at ./IVR_TEST.pl line 13.
Ambiguous use of % resolved as operator % at ./IVR_TEST.pl line 13.
Substitution pattern not terminated at ./IVR_TEST.pl line 13.
The script that i am using is;
Code:
#!/usr/bin/perl -w
use POSIX;
#Directory where the text file will be created.
$directory = '/space/data';
if (!(-d "$directory")) { #check if directory exists - returns true if doesn't exist.
printf "No directory\n", $? >> 8;
exit (1); #exit because directory doesn't exist.
}
my $filename = "$directory/sprintf("%s_%s.TXT", "TIN_WALK", strftime("%Y%m%d", localtime));
print "filename is $filename\n";
open (FILE, ">$filename") || die("Couldn't open file and directory");
print FILE "A,B,C,D,E\n";
close(FILE);
exit(0);
It's showing error here at line 13
Code:
my $filename = "$directory/sprintf("%s_%s.TXT", "DME_TIN_XWALK", strftime("%Y%m%d", localtime));
The OP dies not understand so lets elaborate just a bit. In a nutshell when you open a file for writing regardless of language as filename.txt it will create that file in the current working directory i.e. the directory where you ran your program. If you want to create that file in another location then filename.txt must include the path.
The basic syntax for creating a file for writing is:
open( FILEHANDLE, '>filename')
In your case you want to create a file in /space/data so:
open( FILEHANDLE, '>/space/data/filename')
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.