从终端验证是否安装了python 3

ale*_*r17 10 bash terminal python-3.x

我正在编写一个shell脚本,在脚本运行之前,我想验证用户是否安装了Python 3.有没有人知道或有任何关于如何检查它的想法,输出是一个布尔值?

Eug*_*ash 23

交互式shell

简单地跑python3 --version.Python 3.7.1如果安装了Python 3,你应该得到一些输出.

Shell脚本

使用command内置:

if command -v python3 &>/dev/null; then
    echo Python 3 is installed
else
    echo Python 3 is not installed
fi
Run Code Online (Sandbox Code Playgroud)

正如评论中已经提到的那样,避免which因为它需要启动外部进程,并且在某些情况下可能会给出错误的输出.


Nis*_*tty 5

执行以下命令。 which python3并检查命令的退出状态$?。如果用户安装了 python 3,则为 0,否则为 1。