Detect python version in shell script

前端 未结 14 2104
清歌不尽
清歌不尽 2020-12-07 20:26

I\'d like to detect if python is installed on a Linux system and if it is, which python version is installed.

How can I do it? Is there something more graceful than

14条回答
  •  既然无缘
    2020-12-07 21:07

    Detection of python version 2+ or 3+ in a shell script:

    # !/bin/bash
    ver=$(python -c"import sys; print(sys.version_info.major)")
    if [ $ver -eq 2 ]; then
        echo "python version 2"
    elif [ $ver -eq 3 ]; then
        echo "python version 3"
    else 
        echo "Unknown python version: $ver"
    fi
    

提交回复
热议问题