ddrew78 has asked for the wisdom of the Perl Monks concerning the following question:

Dear monks, I am attempting to write a script using two input files. Would the below code to it for me, or should I wirte it in a different form? Any input is greatly appreciated.
system 'cat temp3 | grep "hdlcppp" > hdlc1'; system "mv hdlc1 hdlc"; system 'cat hdlc | cut -d " " -f2-3 > hdlc1'; system "mv hdlc1 hdlc"; system 'cat hdlc | cut -d " " -f1 > hdlc1'; system 'cat hdlc | cut -d " " -f2 > hdlc2'; system "rm hdlcget"; open(HDCLGET, ">hdlcget"); print HDLCGET "\#\!\/usr\/local\/bin\/expect\n"; print HDLCGET "set timeout -1\n"; open(MYINPUTFILE, "hdlc1"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print HDLCGET "spawn \ ssh $line\n"; print HDLCGET "expect \"assword: \"\n"; print HDLCGET "send \"password\\r\"\n"; print HDLCGET "expect \"#\"\n"; print HDLCGET "send \"terminal length 0\\r\"\n"; print HDLCGET "expect \"#\"\n"; open(MYINPUTFILE1, "hdlc2"); while(<MYINPUTFILE1>) { my($line1) = $_; chomp($line1); print HDLCGET "show xconnect all | inc $line1\\r\"\nexpect \"# +\"\n"; } } print HDLCGET "send \"exit\\r\"\n"; print HDLCGET "interact\n"; system "touch hdlcget"; system "chmod 777 hdlcget"; system "hdlcget >hdlc3";
Another idea I was considering was to just leave everytihng in the original file (hdlc) and then write the script so it uses field one of "$line" for the first input and field two for the second, but I have yet to figure out how to get that to work as well.

Replies are listed 'Best First'.
Re: using multiple input files
by toolic (Bishop) on Mar 27, 2009 at 16:16 UTC
    Would the below code to it for me
    I think you should answer that question. When you run your code, does it work the way you expect? If not, you should try to explain to us how it deviates from your expected behavior. Since we do not know what's in one of your input files ("temp3"), we can not run your code.

    General suggestions:

    • Use the strictures: use warnings; use strict;
    • Are you trying to win the "useless use of cat award"? How about just system 'grep hdlcppp temp3 > hdlc1';
    • It is good practice to check the status of your open and system calls.
    • If your "hdlc2" file isn't too big, it might be more efficient to slurp it into an array once, rather than having to open it and read it for every line of your "hdlc1" file. This would also avoid potential confusion of using $_ in your nested "while" loops.
    • Your system "touch hdlcget" seems unnecessary.
    • Also consider replacing some system calls with Perl built-ins, such as chmod and unlink to minimize shell invocations. See also UNIX 'command' equivalents in Perl

    Update: added chmod/unlink

Re: using multiple input files
by dHarry (Abbot) on Mar 27, 2009 at 16:09 UTC

    I am attempting to write a script using two input files.

    (Voice from above with echo) You have succeeded my son.

    Would the below code to it for me

    (Voice from above with echo) What are you trying to achieve? A little explanation about the desired functionality could be helpful.

    More seriously: you don't do any error checking, what if one of the issued commands fails? You could put use strict; use warnings; in your script.

    Update
    You don't close the file handles.
    And, hint:
    Name "main::HDCLGET" used only once: possible typo

      it appears I almost succeeded. I have already changed the code to the below, but it still doesn't quite process it the way I want. Here is what I have for the code:
      print HDLCGET "\#\!\/usr\/local\/bin\/expect\n"; print HDLCGET "set timeout -1\n"; open(MYINPUTFILE, "hdlc2"); open(MYINPUTFILE1, "hdlc3"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); while(<MYINPUTFILE1>) { my($line1) = $_; chomp($line1); print HDLCGET "spawn \ ssh $line\n"; print HDLCGET "expect \"assword: \"\n"; print HDLCGET "send \"$tacacspw\\r\"\n"; print HDLCGET "expect \"#\"\n"; print HDLCGET "send \"terminal length 0\\r\"\n"; print HDLCGET "expect \"#\"\n"; print HDLCGET "send \"show xconnect all | inc $line1\\r\"\nexp +ect \"#\"\n"; print HDLCGET "send \"exit\\r\"\n"; print HDLCGET "interact\n"; } } close(MYINPUTFILE); close(MYINPUTFILE1);
      Now, the problem: while it does properly process the second input file (hdlc3), it won't move to to the second line of the first input file (hdlc2). Basically, what I'm trying to do is to have it do is process line of of file 1 and 2, then line two of file 1 and 2, etc. Any ideas?