试试which
命令怎么样?
如果要运行which psql
,但未安装Postgres,则似乎没有输出.您只需让终端提示符准备好接受另一个命令:
> which psql
>
但如果安装了Postgres,您将得到一个回复,其中包含指向Postgres安装位置的路径:
> which psql
/opt/boxen/homebrew/bin/psql
看看man which
,似乎还有一个选项可以帮助你:
-s No output, just return 0 if any of the executables are found, or
1 if none are found.
因此,似乎只要您使用的任何脚本语言都可以执行终端命令,就可以发送which -s psql
并使用返回值来确定是否安装了Postgres.从那里你可以随心所欲地打印结果.
我的机器上安装了postgres,所以我运行以下程序
> which -s psql
> echo $?
0
这告诉我命令返回0,表示在我的机器上找到了Postgres可执行文件.