How do I cd into a directory using perl?

前端 未结 3 698
野性不改
野性不改 2021-01-12 15:45

I am trying the following.. system \"cd directoryfolder\" but it fails, also I try system \"exit\" to leave the terminal but it fails.

相关标签:
3条回答
  • 2021-01-12 15:55

    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.

    What you want for the cd case, is - as bobah points out - the function chdir. For exiting your program, there is a 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.

    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.

    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.

    0 讨论(0)
  • 2021-01-12 15:59

    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.
    
    0 讨论(0)
  • 2021-01-12 16:04

    I always like mention File::chdir for cd-ing. 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. I present a more Perl implementation.

    "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.

    0 讨论(0)
提交回复
热议问题