简体   繁体   English

如何使用 perl 进入目录?

[英]How do I cd into a directory using perl?

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”但它失败了,我也尝试系统“exit”离开终端但它失败了。

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.

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.你不能通过调用system来做这些事情的原因是system将启动一个新进程,执行你的命令,并返回退出状态。 So when you call system "cd foo" you will start a shell process, which will switch to the "foo" directory and then exit.因此,当您调用system "cd foo"时,您将启动一个 shell 进程,该进程将切换到“foo”目录然后退出。 Nothing of any consequence will happen in your perl script.在您的 perl 脚本中不会发生任何后果。 Likewise, system "exit" will start a new process and immediately exit it again.同样, system "exit"将启动一个新进程并立即再次退出。

What you want for the cd case, is - as bobah points out - the function chdir .正如 bobah 指出的那样,您想要的 cd 机箱是 function chdir For exiting your program, there is a function exit .为了退出你的程序,有一个 function 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. 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在您的 perl 脚本中调用exit

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.这是因为您的 perl 脚本又是与终端 shell 不同的进程,并且在不同进程中发生的事情通常不会相互干扰。 This is a feature, not a bug.这是一个功能,而不是一个错误。

If you want things to change in your shell environment, you must issue instructions that are understood and interpreted by your shell.如果您希望在 shell 环境中进行更改,您必须发出 shell 可以理解和解释的指令。 cd is such a builtin command in your shell, as is exit . cd是 shell 中的内置命令, exit也是如此。

I always like mention File::chdir for cd -ing.我总是喜欢为cd -ing 提及File::chdir It allows changes to the working directory which are local to the enclosing block.它允许更改封闭块本地的工作目录。

As Peder mentions, your script is basically all system calls tied together with Perl.正如 Peder 所提到的,您的脚本基本上是与 Perl 捆绑在一起的所有系统调用。 I present a more Perl implementation.我提出了更多的 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.这使用了两个非核心模块(并且Archive::Extract自 Perl v5.9.5 起才成为核心),因此您可能必须安装它们。 Use the cpan utility (or ppm on AS-Perl) to do this.使用cpan实用程序(或 AS-Perl 上的ppm )来执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM