[英]'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.