-2

The directory of the file is defined.

I want to get the 'test' value using the 'test' command of 7z.

foreach $str0 (glob "*.zip"){
    my $test = system( "7z t -y $str0");
    print $test;
}

How can I get the '7z test' value?


Edit:

Do you mean to use qx instead of System?

Is it right that you meant it?

foreach $str0 (glob "*.zip"){
    my $test = qx/7z t -y $str0/;
    print $test;
}

or

foreach $str0 (glob "*.zip"){
    my $test = `7z t -y $str0`;
    print $test;
}

I tried both, but I can't get the 'test value'.

TLP
  • 66,756
  • 10
  • 92
  • 149
Yeom
  • 37
  • 4
  • Use backticks/`qx` instead of `system`? – Shawn Mar 08 '22 at 01:33
  • Is this on Windows? – Håkon Hægland Mar 08 '22 at 08:16
  • Are you sure that the command `7z t -y somefile.zip` gives text output? What does it do when you run it in the command prompt? There are basically two things that can go wrong here: 1) The command does not give output to STDOUT, 2) The glob `*.zip` does not give a file name. Or possibly 3) The command gives output to STDERR, which is not captured with `qx` or backticks. (Yes, its basically point 1, but with a twist that can seem that the command does give output in the terminal) – TLP Mar 08 '22 at 11:53
  • What do you mean by the "value"? Do you need the output from stdout, or do you simply need to check the exit code to see if there was an error? If you just need the exit code, then you can run it with system and simply check if $? is non-zero. – Rob Mar 08 '22 at 15:10
  • If there is no problem with the test, I want to decompress it. – Yeom Mar 10 '22 at 04:06

1 Answers1

1

There are at least three ways to do this in Perl; the qx and backticks ways are basically the same thing, except with qx you have a choice of what delimiter to use to start/end the string. Using ' as the delimiter prevents variable interpolation.

The third way is using open to open a pipe to 7zip.

#with qx
my $test = qx/7z t -y $str0/;

#with backticks
my $test = `7z t -y $str0`;

#with open
open my $pipe, '-|', '7z', 't', '-y', $str0;
my $test = join "\n", readline $pipe;
close $pipe;
Nathan Mills
  • 2,243
  • 2
  • 9
  • 15