opening file for appending doesn't work for me

Discussion in 'Perl Misc' started by Ron Eggler, Mar 18, 2008.

  1. Ron Eggler

    Ron Eggler Guest

    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!
    --
    chEErs roN
    Ron Eggler, Mar 18, 2008
    #1
    1. Advertising

  2. Ron Eggler <> writes:

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


    sysopen doesn't work like that.

    See perldoc -f sysopen


    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Mar 18, 2008
    #2
    1. Advertising

  3. Ron Eggler

    Guest

    Ron Eggler <> wrote:
    > 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.
    , Mar 18, 2008
    #3
  4. Ron Eggler wrote:
    >
    > I would like to open a file for appending content and create it if it
    > doesn't exist.


    <snip>

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


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

    See "perldoc -f sysopen".

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Mar 18, 2008
    #4
  5. Ron Eggler

    Ron Eggler Guest

    Ron Eggler wrote:

    > 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! :)
    --
    chEErs roN
    Ron Eggler, Mar 18, 2008
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. scopp
    Replies:
    3
    Views:
    423
    Guest
    Jan 22, 2004
  2. Marcelo
    Replies:
    5
    Views:
    10,181
    Gordon Beaton
    Oct 25, 2005
  3. MZ
    Replies:
    7
    Views:
    812
    Ed Mullen
    Mar 17, 2008
  4. Tilman
    Replies:
    0
    Views:
    403
    Tilman
    Mar 19, 2008
  5. fniles
    Replies:
    0
    Views:
    262
    fniles
    Apr 26, 2009
Loading...

Share This Page