for 循环问题,ls 无法正常工作

for 循环问题,ls 无法正常工作

我有一个必须通过 Ubuntu 运行的代码。当我的代码到达文件列表时,它给出的不是文件列表内的所有子文件夹,而是与主题列表相同的列表……为什么会发生这种情况?这是我的文件夹的组织:Newdata、主题名称、SEFolders 和要处理或删除的文件。Ubuntu 也无法识别 SE,这些子文件夹的命名方式如下,例如“SE13”使用的是 Perl,而不是 NotePad。

#!/usr/bin/perl
$subj_path = "/mnt/c/Users/alicj/Desktop/NEWData";

chdir "$subj_path";

@subj_list=ls -d *;

print "The current directory has the following subjects @subj_list \n";

foreach(@subj_list)

{
chomp;
$subj_name=$_;
print "Working on subject $subj_name \n";

$current_path = "$subj_path"."$subj_name"."/";
chdir "$current_path";

print "Starting Reconstruction in the current directory >> $current_path \n";

@file_list=`ls -d SE*`;
print "The current directory has the following files @file_list \n";

foreach(@file_list)
{

chomp;
$file_name=$_;
print "Working on folder $file_name \n";

$file_path = "$current_path"."$file_name"."/";
chdir "$file_path";

`rm -r *.nii`;
`rm -r *.json`;

`dcm2nii -b ~/.dcm2nii/dcm2nii.ini IM*`;

}

}

答案1

您的一些脚本没有清楚地表达出来,但如果它能检查出一些地方的错误就会很有帮助。

您可以使用 perl 的文件匹配 - 有关详细信息,请参阅 perldoc:

perldoc -f glob

也许可以使用 grep 扩展列表来过滤 glob 的返回值(这里是文件):

grep { -f $_ } <SE*>

另请考虑:

perldoc -f system

产生dcm2nii命令:

system("dcm2nii -b ~/.dcm2nii/dcm2nii.ini ".join(" ",<IM*>))

您也没有要求进行代码审查,但我警告不要盲目地在反引号内执行 rm 命令(使用 unlink 而不是分叉子 shell 并运行 rm),请参阅:

perldoc -f unlink

我还打算建议启用 perl 的警告和严格检查,但这可能超出了需要

use strict;
use warnings;

相关内容