Linux Shell Script: Detect and Select Python 3.x Version
#!/bin/bash
if [ -x '$(command -v python3.8)' ]; then python_version='3.8' elif [ -x '$(command -v python3.9)' ]; then python_version='3.9' elif [ -x '$(command -v python3.10)' ]; then python_version='3.10' fi
if [ -z "$python_version" ]; then echo "No Python 3.x version found on the system" else python_count=$(echo "$python_version" | wc -w) if [ "$python_count" -gt 1 ]; then echo "Multiple Python 3.x versions detected on the system: $python_version" read -p "Please enter the version of Python you want to use: " selected_version if [[ ! "$python_version" =~ "$selected_version" ]]; then echo "Invalid input. Please select from the following versions: $python_version" else echo "Selected Python version: $selected_version" fi else echo "Python $python_version is installed on the system" fi fi
This script checks for the presence of Python 3.x versions and prompts the user to select one if there are multiple versions installed. If only one version is found, it displays the version number on the console.
原文地址: https://www.cveoy.top/t/topic/oiPX 著作权归作者所有。请勿转载和采集!