【发布时间】:2013-11-02 05:07:42
【问题描述】:
我编写了产生一组单词的代码
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`;
}
【问题讨论】:
-
=运算符是一个赋值运算符。您的条件是将$uptime设置为等于$not。我确定这不是你想要的。在 Perl 中,当您想要比较两个字符串是否相等时,您可以使用eq运算符:if( $uptime eq $not ) { ...。 -
我试图做的是下面的行给出一个结果“未运行”> $uptime =
command | sed -n "xp" | cut -d" " -fx;当不运行发生时,我想执行 if 语句中的内容。我认为比较两个字符串可以让我执行 if 语句。但是,当我使用 >if( $uptime eq $not ) 时,脚本不起作用。
标签: linux perl if-statement sed