-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