Linux 脚本判断并选择 Python 版本运行
Linux 脚本判断并选择 Python 版本运行
本脚本用于检测 Linux 系统中是否安装了 Python 3.8、3.9 或 3.10 版本,并根据安装情况提示用户选择使用哪个版本运行脚本。
脚本代码:
#!/bin/bash
if [ -x "$(command -v python3.8)" ];then
python3.8=true
fi
if [ -x "$(command -v python3.9)" ]; then
python3.9=true
fi
if [ -x "$(command -v python3.10)" ]; then
python3.10=true
fi
if [ "$python3.8" = true ] && [ "$python3.9" = true ] && [ "$python3.10" = true ]; then
echo "系统中安装了Python 3.8、Python 3.9 和 Python 3.10 版本,请选择您要使用的Python版本:"
read version
case $version in
"3.8")
python3.8 your_script.py
;;
"3.9")
python3.9 your_script.py
;;
"3.10")
python3.10 your_script.py
;;
*)
echo "输入的版本号不正确,请重新输入"
;;
esac
elif [ "$python3.8" = true ]; then
python3.8 your_script.py
elif [ "$python3.9" = true ]; then
python3.9 your_script.py
elif [ "$python3.10" = true ]; then
python3.10 your_script.py
else
echo "系统中未安装Python 3.8、Python 3.9 或 Python 3.10 版本"
fi
脚本说明:
- 脚本使用
command -v命令检测系统中是否安装了 Python 3.8、3.9 或 3.10 版本,并根据检测结果将对应变量设置为true。 - 如果系统中安装了多个 Python 版本,脚本会提示用户选择要使用的版本,并根据用户输入执行对应版本的 Python 脚本。
- 如果系统中没有安装任何指定版本的 Python,脚本会提示用户系统中未安装对应版本的 Python。
使用示例:
将上述脚本保存为 choose_python.sh 文件,并赋予执行权限:
chmod +x choose_python.sh
运行脚本:
./choose_python.sh
注意:
- 将
your_script.py替换为实际要运行的 Python 脚本文件名。 - 脚本仅支持选择 Python 3.8、3.9 或 3.10 版本,如有需要可以根据实际需求进行修改。
总结:
本脚本提供了一种简单的方式来判断 Linux 系统中是否安装了指定版本的 Python,并根据安装情况选择使用哪个版本运行脚本。
原文地址: https://www.cveoy.top/t/topic/oiP0 著作权归作者所有。请勿转载和采集!