获取python版本_从python获取java版本号

I need to get the java version number, for example "1.5", from python (or bash).

I would use:

os.system('java -version 2>&1 | grep "java version" | cut -d "\\\"" -f 2')

But that returns 1.5.0_30

It needs to be compatible if the number changes to "1.10" for example.

I would like to use cut or grep or even sed.

It should be in one line.

解决方案

Considering an output like this:

$ java -version

java version "1.8.0_25"

Java(TM) SE Runtime Environment (build 1.8.0_25-b17)

Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

You can get the version number with awk like this:

$ java -version 2>&1 | awk -F[\"_] 'NR==1{print $2}'

1.8.0

Or, if you just want the first two .-separated digits:

$ java -version 2>&1 | awk -F[\"\.] -v OFS=. 'NR==1{print $2,$3}'

1.8

Here, awk sets the field separator to either " or _ (or .), so that the line is sliced in pieces. Then, it prints the 2nd field on the first line (indicated by NR==1). By setting OFS we indicate what is the output field separator, so that saying print $2, $3 prints the 2nd field followed by the 3rd one with a . in between.

To use it in Python you need to escape properly:

>>> os.system('java -version 2>&1 | awk -F[\\\"_] \'NR==1{print $2}\'')

1.8.0

>>> os.system('java -version 2>&1 | awk -F[\\\"\.] -v OFS=. \'NR==1{print $2,$3}\'')

1.8

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python可以通过比较版本号的大小来判断它们的顺序。有多种方法可以实现这个功能。引用\[1\]中的代码使用了一个类来比较两个版本号的大小。它首先将版本号转换为列表,并使用0补齐使两个列表的长度相等。然后对列表进行排序,并根据排序后的结果判断版本号的大小。如果两个版本号相等,则输出相等的信息;如果第一个版本号小于第二个版本号,则输出小于的信息;否则输出大于的信息。 引用\[2\]中的代码也是用来比较两个版本号的大小。它首先定义了一个函数来去除版本号中的前导0,并将大版本按"."切割为子版本列表。然后通过比较两个版本号列表的元素来判断它们的大小。如果两个版本号相等,则返回0;如果第一个版本号小于第二个版本号,则返回-1;否则返回1。 引用\[3\]中的代码也是用来比较两个版本号的大小。它将版本号转换为列表,并通过循环比较列表中的元素来判断它们的大小。如果两个版本号相等,则继续比较下一个元素;如果第一个版本号小于第二个版本号,则返回-1;否则返回1。最后,根据列表的长度来判断哪个版本号更高。 综上所述,以上三种方法都可以用来比较两个版本号的大小。你可以根据自己的需求选择其中一种方法来使用。 #### 引用[.reference_title] - *1* [python判断2个版本号大小](https://blog.csdn.net/lizy0327/article/details/121164549)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [华为OD机试 - 比较两个版本号的大小(Java & JS & Python)](https://blog.csdn.net/qfc_128220/article/details/128179974)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python 比较两个版本号大小](https://blog.csdn.net/tinyjm/article/details/93514261)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值