5

perl を使用してフォルダーを作成したいのですが、同じフォルダー内に perl スクリプトが存在します。の入力パラメーターを必要とする FolderCreator.pl を作成しましたfolder name

unless(chdir($ARGV[0]){ # If the dir available change current , unless
    mkdir($ARGV[0], 0700);                             # Create a directory
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";    # Then change or stop
}

これは、scipt が存在する同じフォルダーで scipt を呼び出した場合にのみ正常に機能しました。別のフォルダで呼び出された場合、動作しません。

例えば。

.../Scripts/ScriptContainFolder> perl FolderCreator.pl New
.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl New

最初のものは正常に機能しますが、2 つ目は機能しません。これらのフォルダを作成する方法はありますか?

4

3 に答える 3

8

$Bin 変数を提供するFindBinモジュールを使用できます。スクリプト bin ディレクトリへのフル パスを見つけて、bin ディレクトリからの相対パスを使用できるようにします。

use FindBin qw($Bin);

my $folder = "$Bin/$ARGV[0]";

mkdir($folder, 0700) unless(-d $folder );
chdir($folder) or die "can't chdir $folder\n";
于 2011-05-11T06:25:08.303 に答える
4

タイプミスがあることを除いて、書かれているとおりに機能すると思います。つまり、閉じ括弧がありませんchdir

unless(chdir($ARGV[0])) {   #fixed typo
    mkdir($ARGV[0], 0700);
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
}

スクリプトは次のように実行されます。

  1. スクリプトが $ARGV[0] に chdir できない場合:
  2. パーミッション マスク 0700 を使用して、ディレクトリ $ARGV[0] を作成します。
  3. 作業ディレクトリを $ARGV[0] に変更するか、"cant chdir.." というエラー テキストでスクリプトを終了します。

スクリプトの開始ディレクトリは、そのディレクトリが何であれ、呼び出されたディレクトリになります。*nix では$ENV{PWD}、スクリプト内の変数になります。権限のある任意のフォルダに新しいフォルダを作成します。

この振る舞いは論理的であり、そうあるべきだと思います。例を機能させたい場合は、次のようにします。

.../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New

次のような絶対パスを使用することもできます。

?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New

ETA: ああ、もちろん、どんなに小さくても、常にこれをスクリプトに入れる必要があります。

use strict;
use warnings;
于 2011-05-11T06:41:57.230 に答える
1

私は仕事をして、ここにコードがあります...助けてくれてありがとう...

#!usr/bin/perl

###########################################################################################
# Needed variables
use File::Path;
use Cwd 'abs_path';
my $folName     = $ARGV[0];
#############################################################################################
# Flow Sequence 
if(length($folName) > 0){
    # changing current directory to the script resides dir
    $path = abs_path($0);
    $path = substr($path, 0, index($path,'FolderCreator.pl') );
    $pwd = `pwd`;
    chop($pwd);
    $index = index($path,$pwd);
    if( index($path,$pwd) == 0 ) {
        $length = length($pwd);
        $path = substr($path, $length+1);

        $index = index($path,'/');
        while( $index != -1){
            $nxtfol = substr($path, 0, $index);
            chdir($nxtfol) or die "Unable to change dir : $nxtfol"; 
            $path = substr($path, $index+1);
            $index = index($path,'/');
        } 
    }
    # dir changing done...

    # creation of dir starts here
    unless(chdir($folName)){        # If the dir available change current , unless
        mkdir("$USER_ID", 0700);    # Create a directory
        chdir($folName) or $succode = 3;    # Then change or stop
    }
}
else {
    print "Usage : <FOLDER_NAME>\n";    
}
exit 0;
于 2011-05-12T10:42:05.253 に答える