【问题标题】:How do I cd into a directory using perl?如何使用 perl 进入目录?
【发布时间】:2011-10-23 22:56:28
【问题描述】:

我正在尝试以下.. 系统“cd 目录文件夹” 但它失败了,我也尝试系统“退出”以离开终端但它失败了。

【问题讨论】:

    标签: linux perl ubuntu terminal


    【解决方案1】:

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

    正如 Peder 所提到的,您的脚本基本上是所有与 Perl 绑定在一起的系统调用。我提出了一个更 Perl 的实现。

    "wget download.com/download.zip"; 
    system "unzip download.zip" 
    chdir('download') or die "$!"; 
    system "sh install.sh";
    

    变成:

    #!/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
    

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

    【讨论】:

      【解决方案2】:

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

      您想要的 cd 案例是 - 正如 bobah 所指出的 - 函数 chdir。退出你的程序,有一个函数exit

      但是 - 这些都不会影响您所在的终端会话的状态。在您的 perl 脚本完成后,您的终端的工作目录将与您开始之前相同,您将无法退出通过在 perl 脚本中调用 exit 来进行终端会话。

      这是因为你的 perl 脚本又是一个独立于终端 shell 的进程,在不同进程中发生的事情通常不会相互干扰。这是一项功能,而不是错误。

      如果你想改变你的 shell 环境,你必须发出你的 shell 可以理解和解释的指令。 cd 是你 shell 中的内置命令,exit 也是如此。

      【讨论】:

        【解决方案3】:

        代码:

        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.
        

        【讨论】:

        • 我输入了 'chdir('folder01') 行或死 "$!";'在我的解压缩线之后,但我收到以下错误。 it.pl 第 6 行,“system”附近的语法错误 it.pl 的执行由于编译错误而中止。
        • @sirplzmywebsitelol 您的“解压缩线”在这种情况下没有意义。您能否使用或多或少完整的代码 sn-p 更新您的问题,以便我们可以看到您正在尝试做什么?
        • system "wget download.com/download.zip" system "unzip download.zip" chdir('download') or die "$!";系统“sh install.sh”;
        • 您在system 之后、chdir 之前缺少一个分号。
        • 顺便说一下,一个几乎完全由system 调用组成的perl 脚本应该是一个shell 脚本。直接在命令行上尝试以下操作:wget download.com/download.zip; unzip download.zip; cd download; sh install.sh.
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-07-04
        • 2019-11-14
        • 2011-01-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多