Linux 如何使用 perl cd 进入目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7009055/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I cd into a directory using perl?
提问by sirplzmywebsitelol
I am trying the following.. system "cd directoryfolder" but it fails, also I try system "exit" to leave the terminal but it fails.
我正在尝试以下..系统“cd directoryfolder”但它失败了,我也尝试系统“退出”离开终端但它失败了。
回答by bobah
Code:
代码:
chdir('path/to/dir') or die "$!";
Perldoc:
Perldoc:
chdir EXPR
chdir FILEHANDLE
chdir DIRHANDLE
chdir Changes the working directory to EXPR, if possible. If EXPR is omitted,
changes to the directory specified by $ENV{HOME}, if set; if not, changes to
the directory specified by $ENV{LOGDIR}. (Under VMS, the variable
$ENV{SYS$LOGIN} is also checked, and used if it is set.) If neither is set,
"chdir" does nothing. It returns true upon success, false otherwise. See the
example under "die".
On systems that support fchdir, you might pass a file handle or directory
handle as argument. On systems that don't support fchdir, passing handles
produces a fatal error at run time.
回答by Peder Klingenberg
The reason you can't do those things by calling system
is that system
will start a new process, execute your command, and return the exit status. So when you call system "cd foo"
you will start a shell process, which will switch to the "foo" directory and then exit. Nothing of any consequence will happen in your perl script. Likewise, system "exit"
will start a new process and immediately exit it again.
你不能通过调用来做这些事情的原因system
是这system
将启动一个新进程,执行你的命令,并返回退出状态。因此,当您调用时,system "cd foo"
您将启动一个 shell 进程,该进程将切换到“foo”目录,然后退出。在您的 perl 脚本中不会发生任何后果。同样,system "exit"
将启动一个新进程并立即再次退出。
What you want for the cd case, is - as bobah points out - the function chdir
. For exiting your program, there is a function exit
.
你想要的 cd 盒是 - 正如 bobah 指出的 - 函数chdir
. 为了退出你的程序,有一个函数exit
。
However - neither of those will affect the state of the terminal session you are in. After your perl script finishes, the working directory of your terminal will be the same as before you started, and you will not be able to exit the terminal session by calling exit
in your perl script.
但是 - 这些都不会影响您所在终端会话的状态。在您的 perl 脚本完成后,您终端的工作目录将与您开始之前相同,您将无法通过以下方式退出终端会话调用exit
你的 perl 脚本。
This is because your perl script is again a separate process from your terminal shell, and things that happen in separate processes generally do not interfere with each other. This is a feature, not a bug.
这是因为您的 perl 脚本又是一个独立于终端 shell 的进程,并且在不同进程中发生的事情通常不会相互干扰。这是一个功能,而不是一个错误。
If you want things to change in your shell environment, you must issue instructions that are understood and interpreted by your shell. cd
is such a builtin command in your shell, as is exit
.
如果您希望在您的 shell 环境中发生变化,您必须发出由您的 shell 理解和解释的指令。 cd
在你的 shell 中是这样一个内置命令,就像exit
.
回答by Joel Berger
I always like mention File::chdir
for cd
-ing. It allows changes to the working directory which are local to the enclosing block.
我总是喜欢提到File::chdir
的cd
-ing。它允许对封闭块本地的工作目录进行更改。
As Peder mentions, your script is basically all system calls tied together with Perl. I present a more Perl implementation.
正如 Peder 所提到的,您的脚本基本上是与 Perl 联系在一起的所有系统调用。我提出了一个更多的 Perl 实现。
"wget download.com/download.zip";
system "unzip download.zip"
chdir('download') or die "$!";
system "sh install.sh";
becomes:
变成:
#!/usr/bin/env perl
use strict;
use warnings;
use LWP::Simple; #provides getstore
use File::chdir; #provides $CWD variable for manipulating working directory
use Archive::Extract;
#download
my $rc = getstore('download.com/download.zip', 'download.zip');
die "Download error $rc" if ( is_error($rc) );
#create archive object and extract it
my $archive = Archive::Extract->new( archive => 'download.zip' );
$archive->extract() or die "Cannot extract file";
{
#chdir into download directory
#this action is local to the block (i.e. {})
local $CWD = 'download';
system "sh install.sh";
die "Install error $!" if ($?);
}
#back to original working directory here
This uses two non-core modules (and Archive::Extract
has only been core since Perl v5.9.5) so you may have to install them. Use the cpan
utility (or ppm
on AS-Perl) to do this.
这使用了两个非核心模块(并且Archive::Extract
自 Perl v5.9.5 以来才成为核心模块),因此您可能需要安装它们。使用该cpan
实用程序(或ppm
在 AS-Perl 上)执行此操作。