猿问

Bash 到 php 正则表达式

我有一个 bash 脚本,它为包含“CONNECT”或“DISCONNECT”的字符串拖尾文件。一旦找到这样的字符串,该字符串就会通过管道传输到 php sript。


这是 bash 脚本:


tail -f -n 1 /var/log/connections | grep -P -0 --line-buffered "\bCONNECTED\b|\bDISCONNECTED\b" | php -f $SCRIPT_DIR/connections.php

这是php脚本:


#!/usr/bin/php

<?php


while ( false !== ( $connection_status = fgets ( STDIN ) ) )

{

    $get_status = preg_match ( "/\bCONNECTED\b|\bDISCONNECTED\b/", @$connection_status, $status_match ) ;


    foreach ( $status_match as $status )

    {

        switch ( $status )

        {

            case "CONNECTED": //If the string that got passed to this script (from the BASH script) contains CONNECTED

            {

                print ( "we are connected\r\n" ) ;

            }

            case "DISCONNECTED": //If the string that got passed to this script (from the BASH script) contains DISCONNECTED

            {

                print ( "we are disconnected\r\n" ) ;

            }

        }

    }

}

?>

DISCONNECT按预期工作,但使用CONNECT,它会同时返回"we are connected"和"we are disconnected"


aluckdog
浏览 146回答 1
1回答
随时随地看视频慕课网APP
我要回答