我有列出要在 unix 上洗掉的檔案/檔案夾的方法。然后我用代碼洗掉它:
for my $line (@linesFiles) {
my $command = "rm '$line';"; # or my $command = "rmdir '$line';";
my ($stdout, $stderr) = capture {
system ($command);
};
}
它在大多數情況下都有效,但有時檔案/檔案夾的名稱不正確,包含引號,例如some\folder\incorrect'name或some\folder\incorrect"name. 我還需要洗掉這些檔案/檔案夾。
但是使用我的代碼我收到 EOF 錯誤或該檔案/檔案夾不存在錯誤。使用qorqq時,引號已從檔案名中洗掉,導致檔案/檔案夾不存在錯誤。
有人可以幫助我修改代碼,以便能夠洗掉包含任何潛在危險(至少在這種情況下)字符的檔案/檔案夾,例如" ' $ { } ?
uj5u.com熱心網友回復:
要構建 shell 命令,您可以使用String::ShellQuote(或Win32::ShellQuote)。
use String::ShellQuote qw( shell_quote );
my $shell_cmd = shell_quote( "rm", "--", $qfn );
system( $shell_cmd );
die( "Couldn't launch shell to unlink \"$qfn\": $!\n" ) if $? == -1;
die( "Shell killed by signal ".( $? & 0x7F )." while trying to unlink \"$qfn\"\n" ) if $? & 0x7F;
die( "Shell exited with error ".( $? >> 8 )." while trying to unlink \"$qfn\"\n" ) if $? >> 8;
但是為什么要涉及一個外殼呢?您可以使用 的多引數形式system。
system( "rm", "--", $qfn );
die( "Couldn't launch rm to unlink \"$qfn\": $!\n" ) if $? == -1;
die( "rm killed by signal ".( $? & 0x7F )." while trying to unlink \"$qfn\"\n" ) if $? & 0x7F;
die( "rm exited with error ".( $? >> 8 )." while trying to unlink \"$qfn\"\n" ) if $? >> 8;
但是為什么要使用外部工具呢。可以unlink用來洗掉檔案。
unlink( $qfn )
or die( "Can't unlink \"$qfn\": $!\n" );
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/453015.html
