file handle and variable issues...

Discussion in 'Perl Misc' started by erik, Nov 13, 2005.

  1. erik

    erik Guest

    I am using expect to get firewall configs and then write them to a FH.
    My issue is the I cannot get the $date to work within the FH, therefore
    it does not write the file.

    Here is my subfunction:

    #############################################################
    sub write_config{

    $date = qx(date +%x);

    $date =~ s/\//-/g;

    print " THE DATE IS: $date";

    #qx(touch /vault/var/ns-config/ns-$date/$device-$date);

    qx(mkdir /vault/var/ns-config/$date/);

    $filename = "/vault/var/ns-config/$date/$device";


    open(IN, ">$filename") || die ("Open Failed: $!\n");

    print IN "$config_capture\n";

    sleep 3;

    #qx(/bin/gzip /vault/var/ns-config/$date/$device-$date);

    };#end sub
    ############################################################

    At run time, I get this...

    THE DATE IS: 11-13-2005
    mkdir: cannot create directory `/vault/var/ns-config/11-13-2005': File
    exists
    sh: line 1: /: is a directory
    Open Failed: No such file or directory

    The mkdir error is expected because I have run this many times. You can
    disregard that. But the issue appears to be an issue with $date in the
    $filename variable.

    If I change $filename from the original $filename =
    "/vault/var/ns-config/$date/$device";

    to

    $filename = "/vault/var/ns-config/11-13-2005/$device";

    It works fine. What is wrong with $date in this context?
    erik, Nov 13, 2005
    #1
    1. Advertising

  2. erik

    Brian Wakem Guest

    erik wrote:

    > I am using expect to get firewall configs and then write them to a FH.
    > My issue is the I cannot get the $date to work within the FH, therefore
    > it does not write the file.
    >
    > Here is my subfunction:
    >
    > #############################################################
    > sub write_config{
    >
    > $date = qx(date +%x);
    >
    > $date =~ s/\//-/g;
    >
    > print " THE DATE IS: $date";
    >
    > #qx(touch /vault/var/ns-config/ns-$date/$device-$date);
    >
    > qx(mkdir /vault/var/ns-config/$date/);
    >
    > $filename = "/vault/var/ns-config/$date/$device";
    >
    >
    > open(IN, ">$filename") || die ("Open Failed: $!\n");


    > sh: line 1: /: is a directory
    > Open Failed: No such file or directory



    What is in $device? Perhaps nothing? Therefore you are trying to
    open /vault/var/ns-config/13-11-2005/, which is a directory?



    --
    Brian Wakem
    Email: http://homepage.ntlworld.com/b.wakem/myemail.png
    Brian Wakem, Nov 13, 2005
    #2
    1. Advertising

  3. Brian Wakem <> wrote in news::

    > erik wrote:
    >
    >> I am using expect to get firewall configs and then write them to a
    >> FH. My issue is the I cannot get the $date to work within the FH,
    >> therefore it does not write the file.
    >>
    >> Here is my subfunction:
    >>
    >> #############################################################
    >> sub write_config{
    >>
    >> $date = qx(date +%x);


    Why spawn an external process when it is simple to do it in Perl?

    >> $date =~ s/\//-/g;


    use POSIX qw( strftime );
    print strftime(q{%d-%m-%Y}, localtime), "\n";

    >> qx(mkdir /vault/var/ns-config/$date/);


    perldoc -f mkdir

    Make sure to check if the call actually succeeded.

    >> $filename = "/vault/var/ns-config/$date/$device";
    >>
    >>
    >> open(IN, ">$filename") || die ("Open Failed: $!\n");

    >
    >> sh: line 1: /: is a directory
    >> Open Failed: No such file or directory

    >
    >
    > What is in $device? Perhaps nothing? Therefore you are trying to
    > open /vault/var/ns-config/13-11-2005/, which is a directory?


    Ditto.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 13, 2005
    #3
  4. erik

    erik Guest

    But if I hard code the $date in the $filename, it works fine. That part
    I can't explain.
    erik, Nov 13, 2005
    #4
  5. erik

    erik Guest

    I can even do this, and it works....


    $date = "11-13-2005";
    erik, Nov 13, 2005
    #5
  6. erik

    erik Guest

    I also printed out the $device as you inquired about.

    print " THE device IS: $device";

    It prints minnesota-ns. I don't think the $device is the issue though,
    it is something with using that dash seperated $date variable in a FH.
    erik, Nov 13, 2005
    #6
  7. erik

    Matt Garrish Guest

    "erik" <> wrote in message
    news:...
    > But if I hard code the $date in the $filename, it works fine. That part
    > I can't explain.
    >


    What the hell are you talking about? If you don't learn to quote some
    context to your messages soon you're not going to find anyone who'll help
    you.

    Matt
    Matt Garrish, Nov 13, 2005
    #7
  8. "Matt Garrish" <> wrote in
    news:iVJdf.88$:

    > "erik" <> wrote in message
    > news:...
    >> But if I hard code the $date in the $filename, it works fine. That
    >> part I can't explain.
    >>

    >
    > What the hell are you talking about? If you don't learn to quote some
    > context to your messages soon you're not going to find anyone who'll
    > help you.


    erik is already in my killfile, I am only following this thread through
    others replies to it. What can I say: I am bored today.

    If erik were to realize that the external date command has a newline at
    the end, erik might either chomp the string returned by date, or wisen
    up, and actually use the method I posted earlier to construct the date
    string in Perl.

    But, sometimes, that is too much to ask of people, apparently.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Nov 13, 2005
    #8
  9. erik

    erik Guest

    I think I found a \n after $date. That must be the issue.
    erik, Nov 13, 2005
    #9
    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. Carlo Filippini

    Tie a file handle to a variable

    Carlo Filippini, Sep 29, 2003, in forum: Perl
    Replies:
    1
    Views:
    436
  2. Dietrich
    Replies:
    1
    Views:
    625
    Joe Smith
    Jul 22, 2004
  3. Leon
    Replies:
    2
    Views:
    507
  4. =?ISO-8859-1?Q?KLEIN_St=E9phane?=
    Replies:
    3
    Views:
    439
    hanumizzle
    Oct 6, 2006
  5. Replies:
    18
    Views:
    651
    Dave Thompson
    Jan 3, 2007
Loading...

Share This Page