[英]'If' 'else' statement in Perl
我寫了產生一組單詞的代碼
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`;
}
'if ($uptime = $not)' and 'if ($uptime eq $not)'
是兩個不同的東西。 eq
是字符串比較運算符,因此當比較相等時它將返回 1,當條件不滿足時返回''
,而if ($uptime = $not)
將在$not
評估為真時返回真,因為您將一個變量分配給另一個使用賦值運算符=
。 所以請更改您的代碼。
your condition will look like the following .
$uptime=chomp($uptime);
if ($uptime eq $not){
//your code
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.