2011-08-10 10 views

答えて

22

コード:

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

私は 'chdir(' folder01 ')という行を入力したか、または「$!」;私の解凍ラインの後に、私は次のエラーを取得します。 it.pl行6、 "system"の近くの構文エラー コンパイルエラーのためにit.plの実行が中止されました。 – sirplzmywebsitelol

+1

@sirplzmywebsitelolあなたの「解凍ライン」はこの文脈で意味をなさない。多かれ少なかれ完全なコードスニペットで質問を更新することができますので、何をしようとしているのかわかりますか? –

+0

システム "wget http://download.com/download.zip" システム "unzip download.zip" chdir( 'download')または "$!"; システム "sh install.sh"; – sirplzmywebsitelol

14

あなたがsystemを呼び出すことで、これらのことを行うことができない理由systemは、新しいプロセスを開始するあなたのコマンドを実行し、返すということです終了ステータス。だからsystem "cd foo"に電話すると、シェルプロセスが起動し、 "foo"ディレクトリに切り替えて終了します。あなたのperlスクリプトで何らかの結果が生じることはありません。同様に、system "exit"は新しいプロセスを開始し、すぐに再び終了します。

あなたがcdケースのために何をしたいのですか? - ボーバが指摘するように、機能chdir。プログラムを終了するには、関数exitがあります。

- どちらも、あなたがいる端末セッションの状態に影響しません。あなたのperlスクリプトが終了したら、端末の作業ディレクトリは起動前と同じになり、終了することはできませんあなたのperlスクリプトでexitを呼び出すことによってターミナルセッション。

これは、あなたのperlスクリプトが端末シェルとは別のプロセスであり、別々のプロセスで起こることがお互いに干渉しないためです。これは機能であり、バグではありません。

シェル環境で変更したい場合は、シェルが理解し解釈した指示を発行する必要があります。 cdはシェルの組み込みコマンドで、exitです。

3

私はいつもFile::chdirのようにcd-です。これは、囲みブロックのローカルな作業ディレクトリへの変更を可能にします。

ペダが言及しているように、あなたのスクリプトは、基本的に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)を使用してください。

関連する問題