-1
我知道这个问题相当微不足道,但仍然卡住了。 我已经使用过system(),exec()和back ticks,但是我的解决方案并不合适。 那么我想要的是,我想从我的Perl脚本运行一个命令。如何在perl脚本中使用linux bash命令?在perl脚本中使用它出现错误
下面是一个例子: -
假设命令我想执行的
/scratch/abc/def/xyz/newfold/newfile.py --action=remove;
check.txt: -
Installation root: /scratch/abc/def/xyz
Install.pl:-
#!/usr/bin/perl
use strict;
use warnings;
use Cwd;
my $now=cwd;
print "############################";
print "*****DELETING THE CURRENT SERVICE*****\n";
my $dir = $ENV{'PWD'};
#my $dir = /scratch/Desktop;
my $inst=`grep -i 'Installation root' $dir/check.txt `;
my $split=`echo "$inst" | awk '{print \$(3)}'`; ## this will give the "/scratch/abc/def/xyz" path
#chdir ($split); //Trying to change the directory so that pwd will become till xyz.
qx(echo "cd $split");
$now=cwd;
print $now;
my $dele = `echo "$split/newfold/newfile.py --action=remove;"`; //Problem is here it gets out from the $split directory and could not go inside and execute the command.
print $dele;
输出预计为: - 它应该进入目录并执行子目录命令。 请建议如何在不退出会话的情况下轻松执行命令行。
'$ inst'包含行尾字符。 'chomp'它在你使用它来设置'$ split'之前。那么你也需要'chomp $ split'。 – mob