2016-08-15 6 views
0

初めてここに質問をして、私は、私は、フォーマットの権利を取得願っています:今

my %config; 
$config{variable1}=epaMQMonitor; 
$config{variable2}=IntroscopeEPAgent; 

#issue the ps grep 
my $command = 'ps -ef |grep -w $config{variable1} |grep -wi $config{variable2} |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt|'; 
print "Command is ps -ef |grep -w $config{variable1} |grep -wi $config{variable2} |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt|\n"; 
print "$command\n"; 
open(COMMAND, $command) 
|| die "Error when executing \"$command\": $!"; 

を、両方のための私の予想出力は次のようになります。その代わり

Command is ps -ef |grep -w epaMQMonitor |grep -wi IntroscopeEPAgent |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt| 

、私はこれを取得しています第二1(これが実行されているものをのように見える):

ps -ef |grep -w $config{variable1} |grep -wi $config{variable2} |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt| 

さらに掘削は、Perlが第1レベルの変数($コを代入していることを明らかにしましたmmand)を内容にしていますが、$ config {variable1}と$ config {variable2}を取り込んでいません($コマンドをパッケージとみなしていると思います)。

私は、出力する必要があります:

open my $command, "ps -ef |grep -w $config{variable1} |grep -wi $config{variable2} |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt|"; 
close($command); 

ただし、これは最後の「またはdie」関数では機能しません。

私の質問、最終的には、最初のメソッドで変数置換を有効にすることは可能ですか?

背景 - これは、現在、ハードコードされている一連のperlスクリプトがあるためです。これらのスクリプトは、スクリプト自体を編集することなく、他のサーバに移植可能にして、探しているものをリストした設定ファイルを見てください(この場合は、サーバー上で稼働している必要があります)。

ありがとうございます!あなたは、変数$command設定ライン

my $command = 'ps -ef |grep -w $config{variable1} |grep -wi $config{variable2} |grep -v grep > /wilyagent/BriansPerlTesting/applytest.txt|'; 

+0

'$ config {variable1} = epaMQMonitor;'は 'epaMQMonitor'を文字列にするか、それとも関数ですか?文字列の場合は、構文エラーがあります。 'strict'と' use warnings'を使うべきです。それは間違っていることを教えてくれるでしょう。すべての文字列を引用する必要があります。 – simbabque

+0

'または' dieと '||の違いもあります。死ぬ。論理演算子は異なる_stickiness_を持っています。一般的に、 '||'型の2項演算子は、 'or'型の論理演算子よりも高い_precedence_を持っています。詳細はhttp://perldoc.perl.org/perlop.htmlを参照してください。 – simbabque

+0

補間問題を解いた後、 '$ config {variable1}'と '$ config {variable2}'をシェルに渡すので、それらの変数のソースを完全に信頼しなければならないことに注意してください。さもなければ、あなたは結局非常に悪い日になります。誰が設定ファイルを制御しますか? – DavidO

答えて

1

。また、一重引用符を使用するので、'...'変数は置換されません。代わりに変数の置換を行うには、二重引用符"..."を使用してください。

+0

ああ...まあ、今私は馬鹿だと感じる!ありがとうdgw! – Kittamaru

関連する問題