Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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脚本执行shell命令_Perl_Shell - Fatal编程技术网

从perl脚本执行shell命令

从perl脚本执行shell命令,perl,shell,Perl,Shell,我想将*.DIF文件重命名为*.SUC文件 但是下面的脚本给出了“sh:bad substitution”错误 我不能使用“重命名”,因为我的操作系统不是Linux $com="for i in *.DIF; do mv \$i \${i/DIF/SUC}; done;"; print $com."\n"; print `$com`; 输出: for i in *.DIF; do mv $i ${i/DIF/SUC}; done; sh:bad substitution如果您在使用Perl的

我想将*.DIF文件重命名为*.SUC文件 但是下面的脚本给出了“sh:bad substitution”错误 我不能使用“重命名”,因为我的操作系统不是Linux

$com="for i in *.DIF; do mv \$i \${i/DIF/SUC}; done;";
print $com."\n";
print `$com`;
输出:

for i in *.DIF; do mv $i ${i/DIF/SUC}; done;

sh:bad substitution

如果您在使用Perl的
重命名时遇到问题,请使用与平台无关的模块来移动和复制文件。它是一个核心模块,因此它应该已经是Perl安装的一部分


如果系统命令在shell中输入时起作用,而不是在Perl中输入,那么最可能的原因是Perl没有使用预期的shell。不过,我们需要更多关于您的系统的信息。

没有必要为您可以在Perl中轻松完成的文件操作买单

以下命令将所有
.dif
扩展文件重命名为
.suc

use strict;
use warnings;

use File::Copy qw(move);

move($_, s/dif$/suc/r) for glob('*.dif');

默认情况下,perl使用的是sh,而不是bash,后者允许{/} 帮助。 最后我用了:

open my $bash_handle, '| bash' or die "Cannot open bash: $!";
print $bash_handle 'for i in *.SUC; do mv $i ${i/SUC/DIF}; done;';

在shell上执行“for i in*.DIF;do mv$i${i/DIF/SUC};done;”时效果很好。为什么不直接使用perl
rename
函数呢?为什么脚本有
SUC
,但输出有
diff
?backticks意味着用sh运行
。但是真的。不要这样做。交叉污染脚本语言是为自己构建一些可怕的bug和无法维护的代码的极好方法。坚持使用一种语言——perl或shell,不管是哪种语言。