我有CI/CD Config,它需要python版本被pyenv设置为默认值。我想要python2 -V输出只显示2.7.18的例子。但是,它没有显示2.7.18,而是显示了全文Python 2.7.18。
但是,当我在python3 python -V中使用它时,它显示了正确的当前python3版本(3.9.0)。
我使用下面的代码来尝试只显示数字:$(python -V | grep -Eo '[0-9]\.[0-9]\.[10-19]')。
并使用pyenv:pyenv global $(python3 -V | grep -Eo '[0-9]\.[0-9]\.[10-19]') $(python -V | grep -Eo '[0-9]\.[0-9]\.[10-19]')设置默认值
所以pyenv $(python3 version) $(python2 version)
如下图所示:
谢谢!
发布于 2020-12-05 16:50:48
一种简单的方法是将字符串Python替换为emtpy字符串(如果存在)。
这里是一个简短的一行
python -V 2>&1| sed -e "s/Python//g" | xargs这将打印python版本,将stderr重定向到stdout,将"Python“替换为"”。不带参数的Xargs返回修剪后的输入字符串。
发布于 2020-12-05 22:43:39
以下是获取版本号的其他几种方法:
# Print 1 word per line, the select the last word:
python -V 2>&1 | xargs -n1 | tail -n1
# Print the last word:
python -V 2>&1 | perl -lane 'print $F[-1];'
# Print the first stretch of 1 or more { digits or periods }:
python -V 2>&1 | grep -Po '[\d.]+'https://stackoverflow.com/questions/65154433
复制相似问题