我的目标是编写一个小 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 中不存在该函数”或其他内容,而不是静默返回命令提示符。谁能提供一些关于如何做到这一点的见解?提前致谢。