in reply to RE: Increment
in thread Increment

Monks, After total defeat, I come asking for assistance to this in my view (which is probably simple) difficult problem. I am attempting to grab the output of a system command, and based on the output, I am attempting to execute a system command on that output. I am attempting to increment the output and delete the drives based on that output. I will show an example of the output of the system command and the code. Third, I will list the errors associated from the code. Thanks--
C:\scripts\NT>tpconfig -l Device Robot Drive Robot Drive Device Type Num Index Type DrNum Status Comment Name Path robot 0 - TLD - - - - \\.\Scsi6 +: drive - 0 dlt 1 UP - Drive0-1 \\.\Tape0 drive - 1 dlt 2 UP - Drive0-2 \\.\Tape1 drive - 2 dlt 3 UP - Drive0-3 \\.\Tape2 drive - 3 dlt 4 UP - Drive0-4 \\.\Tape3 drive - 4 dlt 5 UP - Drive0-5 \\.\Tape4 drive - 5 dlt 6 UP - Drive0-6 \\.\Tape5 drive - 6 dlt 7 UP - Drive0-7 \\.\Tape6 drive - 7 dlt 8 UP - Drive0-8 \\.\Tape7 drive - 8 dlt 9 UP - Drive0-9 \\.\Tape8 drive - 9 dlt 10 UP - Drive0-10 \\.\Tape9 drive - 10 dlt 11 UP - Drive0-11 \\.\Tape1 +0 drive - 11 dlt 12 UP - Drive0-12 \\.\Tape1 +1 drive - 12 dlt 13 UP - Drive0-13 \\.\Tape1 +2 drive - 13 dlt 14 UP - Drive0-14 \\.\Tape1 +3 drive - 14 dlt 15 UP - Drive0-15 \\.\Tape1 +4 drive - 15 dlt 16 UP - Drive0-16 \\.\Tape1 +5 robot 1 - TLD - - - - \\.\Scsi7 +: drive - 16 dlt 17 UP - Drive1-0 \\.\Tape1 +6 drive - 17 dlt 18 UP - Drive1-1 \\.\Tape1 +7 drive - 18 dlt 19 UP - Drive1-2 \\.\Tape1 +8 drive - 19 dlt 20 UP - Drive1-3 \\.\Tape1 +9 drive - 20 dlt 21 UP - Drive1-4 \\.\Tape2 +0 drive - 21 dlt 22 UP - Drive1-5 \\.\Tape2 +1 drive - 22 dlt 23 UP - Drive1-6 \\.\Tape2 +2 drive - 23 dlt 24 UP - Drive1-7 \\.\Tape2 +3 drive - 24 dlt 25 UP - Drive1-8 \\.\Tape2 +4 drive - 25 dlt 26 UP - Drive1-9 \\.\Tape2 +5 drive - 26 dlt 27 UP - Drive1-10 \\.\Tape2 +6 drive - 27 dlt 28 UP - Drive1-11 \\.\Tape2 +7 drive - 28 dlt 29 UP - Drive1-12 \\.\Tape2 +8 drive - 29 dlt 30 UP - Drive1-13 \\.\Tape2 +9 drive - 30 dlt 31 UP - Drive1-14 \\.\Tape3 +0 drive - 31 dlt 32 UP - Drive1-15 \\.\Tape3 +1 #### CODE ##### use strict; my $drive; my $count = -1; open( LIST, "tpconfig -l|" ) or die $!; while(<LIST>) { ($drive) = unpack '@0 A8',$_; ++$count if m/^drive/; system("tpconfig -delete -drive -index ${count}"); } #### ERRORS ######### Cannot delete drive index 0 Cannot delete drive index 0 Cannot delete drive index 17 Cannot delete drive index 32

Replies are listed 'Best First'.
RE: RE: RE: Increment
by chromatic (Archbishop) on Sep 06, 2000 at 06:14 UTC
    I think it's your ++$count that's the problem. The drive numbers are sequential, but you have lines that begin with 'robot'. You don't increment $count for these lines (good) but you do the system command (not so good). Perhaps you should skip out of the loop early if you run into a robot:
    next if /^robot/; $count++; system( ... );
    Without having anything named 'tpconfig' on my system, I can't check the man page for error messages, but I would be surprised if it allowed you to delete a drive you'd just deleted. The numbers seem to be fairly close.

    Update: Here's a more complete code segment that really ought to do it for you:

    my $drive; my $count = 0; open( LIST, "tpconfig -l|" ) or die $!; while(<LIST>) { ($drive) = unpack '@0 A8',$_; next unless /^drive/; (system("tpconfig -delete -drive -index ${count}") == 0) or print +"Error deleting: $?"; $count++; }
    Starts at 0, doesn't execute the command unless it's found a drive. Increments the number after it executes the command, checks $? for errors. (I'm not 100% certain about the syntax of that system checking, but it was in perlfunc that way.)
      The output of $count is still wrong starting at 1 and ending at 34. That will give the tpconfig command a wrong index number and will give errors indicating can't delete drive index. The print of $count is shown below: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      Chromatic, Thanks for the reply, however, it didn't work. But, I used your advice on the next if statement. It appears that when the tpconfig command executes, it puts the header information in there which includes Device, Type and robot. Therefore, the unpack operator picks it up. Therefore, I put multiple next if statements to skip the Device, Type and robot entries. Here is the output of tpconfig command:
      C:\>tpconfig -l Device Robot Drive Robot Drive Device Type Num Index Type DrNum Status Comment Name Path robot 0 - TLD - - - - \\.\Scsi6 +:
      I then used the following to fix the code. If you see a more elegant way of putting multiple next unless statements-- please feel free to flame me. Thanks for your help. The code is shown below:
      sub delete_drives3 { my $drive; $count = -1; open(TPCONFIG,"tpconfig -l|"); while(<TPCONFIG>) { ($drive) = unpack '@0 A5',$_; next if /^r/; next if /^D/; next if /^T/; ++$count; print "Deleting drive $count\n"; system("tpconfig -delete -drive -index ${count}"); }; };