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

Monk, 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: Increment
by Adam (Vicar) on Sep 06, 2000 at 05:40 UTC
    You forgot your closing </code> tag.

    use strict; my $drive; my $count = -1; open( LIST, "tpconfig -l|" ) or die $!; while( <LIST> ) # I added the <LIST>... which I suspect you had. { ($drive) = unpack '@0 A8',$_; ++$count if m/^drive/; system("tpconfig -delete -drive -index ${count}"); # You should check all system calls for error. # system will set $? to an error code for you. }
    As for your errors... they don't look Perl related. Perhaps there is some other reason that you can't delete a drive... like some process has some kind of lock on a file in the drive.

    Update: You can read up on $? here

    Another Update: raj8 says the script deletes the drives, but still generates the errors.
    The "problem" is that system doesn't capture STDERR. The way to circumvent this is to redirect to nul: BUT BEWARE! This will hide ALL error messages from the system call!
    Try this:

    system("tpconfig -delete -drive -index ${count} 2>nul:"); die "Unknown Error with tpconfig $count" if $? >> 8;
    Also, be sure to read chromatic's post. He raises a good point about when you make the system call, although I suspect that since there are only two robots and four errors that this isn't the complete answer. (Although I would bet good money that it does explain the duplicity of the index 0 error).
      Correct--this index 0 is hard to crack... Here is the output of $count:
      1 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
RE: Increment
by eLore (Hermit) on Sep 06, 2000 at 05:41 UTC
    Please, Please, Please wrap code in tags...

    formatting tips

    That said, edit the post, and I'd be happy to look at it again...

      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
        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.)