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

标签 bash python-3.x terminal

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

最佳答案

交互式外壳

只需运行 python3 --version。如果安装了 Python 3,您应该会得到类似 Python 3.8.1 的输出。

外壳脚本

您可以使用commandtype 内置函数:

command -v python3 >/dev/null 2>&1 && echo Python 3 is installed  # POSIX-compliant
type -P python3 >/dev/null 2>&1 && echo Python 3 is installed     # Bash only

不推荐使用 which,因为它需要启动外部进程,并且可能会在 some cases 中给出不正确的输出。 .

关于bash - 从终端验证是否安装了 python 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38485373/

相关文章:

terminal - 如何从终端更新jenkins插件?

python - 如何编写 django shell 操作脚本?

unit-testing - Elasticsearch “get by index”返回文档,而 “match_all”不返回结果

python - 从迭代器创建容器

linux - 从当前终端在新终端中运行 shell 脚本

java - 调用外部类和方法

mysql - 把MySQL命令输出放到一个变量里,也打印在屏幕上?

bash - 查找包含 1 个字符串但不包含另一个字符串的所有文件名

string - shell 脚本 : How to check if variable is null or no

python - 使用 Python 抓取时丢失数据?