如何使用perl进入目录?

时间:2022-05-05 22:36:52

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

3 个解决方案

#1


23  

Code:

chdir('path/to/dir') or die "$!";

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.

#2


14  

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.

你不能通过调用系统来做这些事情的原因是系统将启动一个新进程,执行你的命令,并返回退出状态。因此,当您调用系统“cd foo”时,您将启动一个shell进程,该进程将切换到“foo”目录然后退出。在perl脚本中不会发生任何后果。同样,系统“退出”将启动一个新进程并立即再次退出。

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。要退出程序,有一个功能退出。

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脚本完成后,终端的工作目录将与您开始之前的工作目录相同,并且您将无法退出终端会话在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. This is a feature, not a bug.

这是因为你的perl脚本又是一个与你的终端shell分开的进程,而在不同进程中发生的事情通常不会相互干扰。这是一个功能,而不是一个bug。

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中的内置命令,就像退出一样。

#3


3  

I always like mention File::chdir for cd-ing. It allows changes to the working directory which are local to the enclosing block.

我总是喜欢提到用于cd-ing的File :: chdir。它允许更改封闭块本地的工作目录。

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.

这使用了两个非核心模块(自从Perl v5.9.5起,Archive :: Extract只是核心),因此您可能需要安装它们。使用cpan实用程序(或AS-Perl上的ppm)执行此操作。

#1


23  

Code:

chdir('path/to/dir') or die "$!";

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.

#2


14  

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.

你不能通过调用系统来做这些事情的原因是系统将启动一个新进程,执行你的命令,并返回退出状态。因此,当您调用系统“cd foo”时,您将启动一个shell进程,该进程将切换到“foo”目录然后退出。在perl脚本中不会发生任何后果。同样,系统“退出”将启动一个新进程并立即再次退出。

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。要退出程序,有一个功能退出。

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脚本完成后,终端的工作目录将与您开始之前的工作目录相同,并且您将无法退出终端会话在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. This is a feature, not a bug.

这是因为你的perl脚本又是一个与你的终端shell分开的进程,而在不同进程中发生的事情通常不会相互干扰。这是一个功能,而不是一个bug。

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中的内置命令,就像退出一样。

#3


3  

I always like mention File::chdir for cd-ing. It allows changes to the working directory which are local to the enclosing block.

我总是喜欢提到用于cd-ing的File :: chdir。它允许更改封闭块本地的工作目录。

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.

这使用了两个非核心模块(自从Perl v5.9.5起,Archive :: Extract只是核心),因此您可能需要安装它们。使用cpan实用程序(或AS-Perl上的ppm)执行此操作。