如何为传递给lynx的URL无效时设置默认操作?

时间:2013-10-28 23:12:09

标签: linux bash

我的目标是编写一个小的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中不存在该函数”之类的消息,而不是静默地返回到命令提示符。任何人都可以提供一些有关如何做到这一点的见解?提前谢谢。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分: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”的别名。