0

我的目标是编写一个小 bash 脚本,将给定 PHP 函数手册页的内容输出到终端。我当前的脚本(pfunc)如下:

#!/bin/bash

if [ -z "$1" ]

    then
        echo
        echo "No function specified"
        echo
        echo "pfunc command syntax: pfunc <function>"
        echo        
        echo "Example: pfunc func_get_args"
        echo
        exit 1

    else    
        func=$1
        url="http://php.net/$func"
        contents=$(lynx -dump $url) 

        clear
        awk -v a="$contents" -v b="$func" 'BEGIN{gsub(/\"\n\"/, "\"\\n\"", a); print substr(a, index(a, b" —"), index(a, "See Also") - index(a, b" —"))}' 

fi

到目前为止,它按预期工作:

me@mybox:~$ pfunc rand | head -17

生产

rand — Generate a random integer

Description

   int rand ( void )
   int rand ( int $min , int $max )

   If called without the optional min, max arguments rand() returns a
   pseudo-random integer between 0 and [74]getrandmax(). If you want a
   random number between 5 and 15 (inclusive), for example, use rand(5,
   15).

     Note: On some platforms (such as Windows), [75]getrandmax() is only
     32767. If you require a range larger than 32767, specifying min and
     max will allow you to create a range larger than this, or consider
     using [76]mt_rand() instead.

每当传递了无效的 URL 时,我想打印一条消息,例如“PHP 中不存在该函数”或其他内容,而不是静默返回命令提示符。谁能提供一些关于如何做到这一点的见解?提前致谢。

4

3 回答 3

1

无论查询是否成功,PHP.net 都会返回成功响应,因此您无法像在行为良好的网站中那样检查 HTTP 状态代码。

您可以改为使用这样的 kludge:

#!/bin/bash

if [ -z "$1" ]

    then
        echo
        echo "No function specified"
        echo
        echo "pfunc command syntax: pfunc <function>"
        echo     
        echo "Example: pfunc func_get_args"
        echo
        exit 1

    else 
        func=$1
        url="http://php.net/$func"
        contents=$(lynx -dump $url)

        if [[ $contents == *"doesn't exist. Closest matches"* ]]
        then 
            echo "No such function" >&2
        else
            clear
            awk -v a="$contents" -v b="$func" 'BEGIN{gsub(/\"\n\"/, "\"\\n\"", a); print substr(a, index(a, b" —"), index(a, "See Also") - index(a, b" —"))}'
        fi
fi
于 2013-10-28T23:30:09.943 回答
0

我会根据你的脚本做什么:

#!/bin/bash

if [ -z "$1" ]

    then
        echo
        echo "No function specified"
        echo
        echo "pfunc command syntax: pfunc <function>"
        echo        
        echo "Example: pfunc func_get_args"
        echo
        exit 1

    else
        func=$1
        url="http://php.net/$func"
        contents=$(lynx -dump $url)
        if [[ $contents =~ $func[[:space:]]+doesn.t[[:space:]]+exist.[[:space:]]+Closest[[:space:]]+matches ]]
        then
            echo >&2 "php.net don't have a page for $func"
            exit 1
        fi
        clear
        awk -v a="$contents" -v b="$func" 'BEGIN{gsub(/\"\n\"/, "\"\\n\"", a); print substr(a, index(a, b" —"), index(a, "See Also") - index(a, b" —"))}'
fi
于 2013-10-28T23:17:51.390 回答
0

您可以解析 lynx 的响应(或其他方式,如 wget、curl、lwp-request)以确定该函数是否不存在。

lwp-request 的一个例子:

[neumann@MacBookPro ~]$ GET "http://php.net/$func"|grep "<b>$func</b> doesn'\t exist"
 <b>toto</b> doesn't exist. Closest matches:
[neumann@MacBookPro ~]$ func=preg_match
[neumann@MacBookPro ~]$ GET "http://php.net/$func"|grep "<b>$func</b> doesn'\t exist"
[neumann@MacBookPro ~]$ 

所以你可以尝试类似的东西:

existFunction(){
    lwp-request -mget "http://php.net/$1"|grep "<b>$1</b> doesn'\t exist" >/dev/null || echo "1"
}

if [[ $(existFunction $1) = 1 ]]; then
    echo "YES"
fi

注意:GET 是“lwp-request -mget”的别名。

于 2013-10-28T23:31:18.993 回答