在perl中创建和读取tar.bz2文件 [英] create and read tar.bz2 files in perl
问题描述
我一直尝试使用
system("tar -jcvf archive_name.tar.bz2 $my_file")
但我收到错误
archive_name.tar.bz2:无法打开:无此类文件或目录
tar:错误无法恢复:立即退出
tar:孩子返回状态2
archive_name.tar.bz2: Cannot open: No such file or directory tar: Error is not recoverable: exiting now tar: Child returned status 2
是否不可能在perl中使用此方法创建.tar.bz2?
Is it not possible to create .tar.bz2 using this method in perl? I would prefer not to use a module but will if it is absolutely necessary
推荐答案
首先,你不是这样做的在Perl中 。你生成一个单独的进程运行命令。
First of all, you're not doing it in Perl. You're spawning a separate process that runs the command.
如果您想在Perl中执行此操作,您需要使用 Archive :: Tar 模块和可能 IO :: Compress :: Bzip2 模块。
If you want to do this in Perl, you would need to use the Archive::Tar module and possibly IO::Compress::Bzip2 module too.
第一件事是看看是否可以直接从命令行运行这个命令:
First thing is to see if you can run this command straight from the command line:
$ tar -jcvf archive_name.tar.bz2 $my_file
如果你不能从命令行运行tar命令,它是不太可能能够从Perl使用 system
命令。
If you can't run the tar command from the command line, it's highly unlikely to be able to run it from inside Perl using the system
command.
运行命令的方式,使用命令和所有参数在单个串中,导致Perl运行命令一个shell(通常 / bin / sh
)。如果在列表中运行带参数的命令,Perl将直接运行命令而不使用shell:
The way you ran the command, with the command and all of the arguments in a single strung, causes Perl to run the command from a shell (usually /bin/sh
). If you run the command with the arguments in a list, Perl will run the command directly without a shell:
my $error = system qw( tar -jcvf archive_name.tar.bz2 $my_file );
$ b < 。 系统命令文档包含有关处理错误的详细信息。
Always check the output of the system command and also the value of $?
. The system command documentation has more information about processing errors.
这篇关于在perl中创建和读取tar.bz2文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!