-2

単語のグループを生成するコードを書きました

my $uptime = `command | sed -n "xp" | cut -d" " -fx`;

上記のコマンドにより、以下の単語が得られます。

not running

以下のようなifステートメントでこの単語を使用したいと思います。

if ($uptime = $not){
    $run = `command`;
    $start = `start`;

変数を宣言して$not入れ"not running"ました。スクリプトは機能していますが、プログラムの実行中は逆です。以下のコマンドは、(「ok」のような) 変数$notにない別の単語を与えますが、スクリプトはプログラムを再起動しています。

#!/usr/bin/perl

use warnings;
use strict;

$not = "not running";
$uptime = `command | sed -n "xp" | cut -d" " -fx`;
if ($uptime = $not){
    # If not running, the program then executes the below, else do nothing
    $run = `cp /home/x`;
    $start = `start`;
}
4

1 に答える 1

2
'if ($uptime = $not)' and 'if ($uptime eq $not)'

2つの異なるものです。eqは文字列比較演算子であるため、比較が等しく''、条件が満たされない場合は 1if ($uptime = $not)を返しますが、 が true に$not評価されると true を返します。代入演算子を使用してある変数を別の変数に代入しているためです=。そのため、コードを変更してください。

your condition will look like the following .
$uptime=chomp($uptime);
if ($uptime eq $not){
//your code
}
于 2013-11-02T05:18:13.740 に答える