opening file for appending doesn't work for me

R

Ron Eggler

Hi,

I would like to open a file for appending content and create it if it
doesn't exist. I have been trying to do all I can but it just wouldn't open
it. My code looks like:

@files = </usr/share/NovaxTSP/logs/*>; #path where the log files are saved
my $key_indicator="----Public key----"; #string that specifies that there
is an ssh public key in the next lines and it will need to be appended to
~/.ssh/authorized_keys
my $key_file="~/.ssh/authorized_keys"; #path the the authorized-keys file

foreach $file (@files) { # read out the directory content
print "parse file ".$file.":\n";
file_parser($file);
}

sub file_parser
{
$file = $_[0]; # get 1st parameter (filename)
open(INFO, $file); #opens file systemstats
@lines = <INFO>; #assigns lines to array
$num=0;
foreach $line (@lines){ #go through each line in file
$num++;
print " ".$num ." - ". $line;
if(rindex($line,$key_indicator)>-1){
print "Dude, append the rest of the file to ~/.ssh/authorized_keys
file\n"; #ah, there's an ssh key, the rest of the file will need to be
appended to ~/.ssh/authorized_keys
sysopen (KEY, ">>$key_file", 0644);
print $lines[$num+1]."\n";
print KEY $lines[$num+1];
print $lines[$num+2]."\n";
print KEY $lines[$num+2];
close(KEY);
}
}
}

I appreciate any kind of help!

Thank you so much!
 
X

xhoster

Ron Eggler said:
Hi,

I would like to open a file for appending content and create it if it
doesn't exist. I have been trying to do all I can but it just wouldn't
open it. My code looks like:

sysopen (KEY, ">>$key_file", 0644);

sysopen does not parse the filename slot to get the mode, so you are trying
to open a file whose name literally starts with two greater than signs.

0644 looks like permission, but it is in sysopen's slot for mode, not
permission.

You aren't checking for success and inspecting $! upon failure.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
G

Gunnar Hjalmarsson

Ron said:
I would like to open a file for appending content and create it if it
doesn't exist.

sysopen (KEY, ">>$key_file", 0644);

use Fcntl;
sysopen KEY, $key_file, O_WRONLY|O_APPEND|O_CREAT, 0644 or die $!;

See "perldoc -f sysopen".
 
R

Ron Eggler

Ron said:
Hi,

I would like to open a file for appending content and create it if it
doesn't exist. I have been trying to do all I can but it just wouldn't
open it. My code looks like:

@files = </usr/share/NovaxTSP/logs/*>; #path where the log files
are saved
my $key_indicator="----Public key----"; #string that specifies
that there is an ssh public key in the next lines and it will need to be
appended to ~/.ssh/authorized_keys
my $key_file="~/.ssh/authorized_keys"; #path the the authorized-keys file

foreach $file (@files) { # read out the directory
content
print "parse file ".$file.":\n";
file_parser($file);
}

sub file_parser
{
$file = $_[0]; # get 1st parameter
(filename)
open(INFO, $file); #opens file systemstats
@lines = <INFO>; #assigns lines to array
$num=0;
foreach $line (@lines){ #go through each line in
file
$num++;
print " ".$num ." - ". $line;
if(rindex($line,$key_indicator)>-1){
print "Dude, append the rest of the file to
~/.ssh/authorized_keys
file\n"; #ah, there's an ssh key, the rest of the file will need to
be appended to ~/.ssh/authorized_keys
sysopen (KEY, ">>$key_file", 0644);
print $lines[$num+1]."\n";
print KEY $lines[$num+1];
print $lines[$num+2]."\n";
print KEY $lines[$num+2];
close(KEY);
}
}
}

I appreciate any kind of help!

Thank you so much!

I realized the problem was that my script wouldn't recognize "~" in the
path. I replaced it with the absolute home path and it worked just fine
with: open (KEY, ">>$key_file") || die "Cannot open file $key_file!\n";
Thanks! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top