Brandon M.
Brandon M.

Reputation: 268

How do I set a default action for when the URL passed to lynx is invalid?

My goal is to write a little bash script that outputs the contents of the given PHP function's manual page to the terminal. My current script (pfunc) is as follows:

#!/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

It's working as expected so far:

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

produces

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.

Whenever an invalid URL is passed, I would like to print a messages like "That function doesn't exist in PHP" or something, instead of silently returning to the command prompt. Could anyone provide a little insight on how to do this? Thanks in advance.

Upvotes: 0

Views: 92

Answers (3)

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185550

What I would do based on your script :

#!/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

Upvotes: 0

Idriss Neumann
Idriss Neumann

Reputation: 3838

You could parse the response of lynx (or others ways like wget, curl, lwp-request) to determine if the function doesn't exist.

An example with 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 ~]$ 

So you could try something like :

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

NB : GET is an alias of "lwp-request -mget".

Upvotes: 0

that other guy
that other guy

Reputation: 123570

PHP.net returns a successful response regardless of whether or not the query was successful, so you can't check the HTTP status code like you would in well behaved web sites.

You can instead use a kludge like this:

#!/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

Upvotes: 1

Related Questions