bash:根据安装的python版本执行不同的操作

bash:根据安装的python版本执行不同的操作,python,bash,shell,Python,Bash,Shell,我必须根据机器上安装的Python版本安装不同的模块 A询问如何执行此操作,但它只将结果打印到屏幕上。例如: $ python -c 'import sys; print sys.version_info' sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0) 或者更确切地说: $ python -c 'import sys; print(".".join(map(str, sys.version

我必须根据机器上安装的Python版本安装不同的模块

A询问如何执行此操作,但它只将结果打印到屏幕上。例如:

$ python -c 'import sys; print sys.version_info'
sys.version_info(major=2, minor=7, micro=3, releaselevel='final', serial=0)
或者更确切地说:

$ python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))'
2.7.3
在bashshell上,如何捕捉上面打印的行,以便将其值合并到if语句中

编辑:好的,现在我意识到这很简单。我一开始被以下问题困扰:

a=$(python --version)

。。。因为它没有为变量a分配任何内容,所以只将版本打印到屏幕上。

您可以为变量分配值:

pyver=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))')
请注意,在=

现在,您可以在if语句中使用它:

if [[ "$pyver" == "2.7.0" ]]; then
    echo "Python 2.7.0 detected"
fi

可以将值指定给变量:

pyver=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))')
请注意,在=

现在,您可以在if语句中使用它:

if [[ "$pyver" == "2.7.0" ]]; then
    echo "Python 2.7.0 detected"
fi
大概是这样的:

if [[ $(python --version 2>&1) == *2\.7\.3 ]]; then
  echo "Running python 2.7.3";
  # do something here
fi
请注意,python版本输出到STDERR,因此需要将其重定向到STDOUT

为了将其分配给变量

a=$(python --version 2>&1)
大概是这样的:

if [[ $(python --version 2>&1) == *2\.7\.3 ]]; then
  echo "Running python 2.7.3";
  # do something here
fi
请注意,python版本输出到STDERR,因此需要将其重定向到STDOUT

为了将其分配给变量

a=$(python --version 2>&1)

使用命令替换``或$:

if [ $(python ...)  == 2.7.3 ]
then
 ...
fi

使用命令替换``或$:

if [ $(python ...)  == 2.7.3 ]
then
 ...
fi

python-version输出到STDERR,因为它只将版本打印到screen.python-version输出到STDERR,因为它只将版本打印到screen.ooh好的,这就是我缺少的。我怎么能自己发现它正在打印到STDERR?仅仅通过猜测和尝试?@RickyRobinson当它没有将输出重定向到变量并打印在屏幕上时,您可以说python-version 2>/dev/null来查看发生了什么。哦,好吧,这就是我缺少的。我怎么能自己发现它正在打印到STDERR?仅仅通过猜测和尝试?@RickyRobinson当它没有将输出重定向到变量并打印在屏幕上时,您可以说python-version 2>/dev/null来查看发生了什么。