Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用perl创建文件夹_Perl_Mkdir - Fatal编程技术网

使用perl创建文件夹

使用perl创建文件夹,perl,mkdir,Perl,Mkdir,我想使用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 "

我想使用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
}
只有当我们调用scit时,它才能正常工作,它位于同一个文件夹中。如果在另一个文件夹中调用,则If不起作用

例如

第一个很好,但第二个不行。有没有办法创建这些文件夹?

您可以使用该模块,该模块为我们提供$Bin变量。它定位到脚本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";

我认为它的工作原理与所写的完全相同,只是您有一个输入错误,即在
chdir
周围缺少一个右括号

unless(chdir($ARGV[0])) {   #fixed typo
    mkdir($ARGV[0], 0700);
    chdir($ARGV[0]) or die "can't chdir $ARGV[0]\n";
}
脚本的运行方式如下:

  • 如果脚本无法chdir到$ARGV[0] 然后:
  • 将目录$ARGV[0]设为 权限掩码为0700
  • 将工作目录更改为 $ARGV[0]或使用 错误文本“铁路超高chdir..”
  • 脚本的起始目录将是从中调用它的目录,不管该目录是什么。在*nix上,这将是脚本中的
    $ENV{PWD}
    变量。它将在任何有权限的文件夹中创建一个新文件夹

    我认为这种行为是合乎逻辑的,也是应该的。如果希望您的示例起作用,请执行以下操作:

    .../Scripts> perl ./ScriptContainFolder/FolderCreator.pl ScriptContainFolder/New
    
    也可以使用绝对路径,例如

    ?> FolderCreator.pl /home/m/me/Scripts/ScriptContainFolder/New
    
    埃塔:哦,你当然应该总是,总是把这个放在你的脚本中,不管有多小:

    use strict;
    use warnings;
    

    我已经完成了任务,这是代码。。。谢谢大家的帮助

    #!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;
    
    #!usr/bin/perl
    ###########################################################################################
    #所需变量
    使用File::Path;
    使用Cwd“abs_路径”;
    我的$folName=$ARGV[0];
    #############################################################################################
    #流序列
    如果(长度($folName)>0){
    #将当前目录更改为脚本驻留目录
    $path=abs\u路径($0);
    $path=substr($path,0,index($path,'FolderCreator.pl');
    $pwd=`pwd`;
    印章($pwd);
    $index=索引($path,$pwd);
    if(索引($path,$pwd)==0){
    $length=长度($pwd);
    $path=substr($path,$length+1);
    $index=索引($path,“/”);
    而($index!=-1){
    $nxtfol=substr($path,0,$index);
    chdir($nxtfol)或die“无法更改目录:$nxtfol”;
    $path=substr($path$index+1);
    $index=索引($path,“/”);
    } 
    }
    #目录更改完成。。。
    #dir的创建从这里开始
    除非(chdir($folName)){#如果可用的目录更改为当前,除非
    mkdir($USER_ID),0700)#创建一个目录
    chdir($folName)或$succode=3;#然后更改或停止
    }
    }
    否则{
    打印“用法:\n”;
    }
    出口0;
    
    btw,我尝试了$path=substr(abs_path($0),0,index(abs_path($0),'FolderCreator.pl')$pwd=
    pwd
    ;印章($pwd)$路径=子路径($path,length($pwd));为了获取相对路径,还尝试了mkpath($path.$ARGV[0]),但它给出了错误“mkdir/Darshana:Permission denied at./Darshana/scripts/FolderCreator.pl line 25”,我的脚本正在生成一些文本文件。所以我想把它们组织到文件夹中。当我想要创建这些文本文件时,我创建文件夹,然后将文件夹更改为该文件夹并创建文本文件。因此FolderCreator.pl MyDir将创建MyDir文件夹并在其中创建这些文本文件。
    #!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;