如何在perl脚本的属性中存储telnet-> cmd的值

时间:2016-04-22 04:01:37

标签: perl grep

我一直在跑

final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
        ... do something here
    }
  }
});

grep -n \"fixed-address $IP_Address\" /etc/dhcp3/dhcpd.conf | cut -d \":\" -f2" 内,我想将输出存储在变量中。我尝试时输出值为telnet->cmd,但输出值应为916.这是我的Perl脚本的一部分

1

请告诉我如何在my $dhcp_value = $telnet->cmd( string => "grep -n \"fixed-address $IP_Address\" /etc/dhcp3/dhcpd.conf | cut -d \":\" -f2" ); print "$dhcp_value\n";

中运行grep -n命令

1 个答案:

答案 0 :(得分:0)

如果您使用的是Net::Telnet模块,那么cmd方法的文档会说明

  

在标量上下文中,从远程端读取的字符将被丢弃,成功时将返回1

     

在列表上下文中,只返回命令生成的输出,每个元素一行。

因此,您需要将列表上下文应用于cmd方法调用。您可以使用数组,例如documnentation中的示例,也可以将标量变量放在括号中

我认为你需要的是这个

请注意,为了清楚起见,我已经单独构建了命令stringf,并使用了qq{...}的替代分隔符,以避免必须转义嵌入式双引号

另请注意,cmd调用的返回值可能会在结尾处添加换行符。 chomp会为您删除这些

my $cmd = qq{grep -n "fixed-address $IP_Address" /etc/dhcp3/dhcpd.conf | cut -d ":" -f2};

my ($dhcp_value) = $telnet->cmd(string => $cmd );
chomp $dhcp_value;

print "$dhcp_value\n";
相关问题