51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

使用Bash shell脚本从文件中解析版本号。

英文:

Parsing version number from file using shell script in Bash

问题 {#heading}

I'm trying to extract a version number from a file with a directory that looks like this:

... /directory/to/java/bin/./java -version

Running that from the command line produces this output:

java version "1.7.0_269"
Java(TM) SE Runtime Environment (build 7.0.8.10 - pkshd34234hsd284842_01(xop4_))
bla bla bla
whole bunch of
extra info

And I need to extract just that version number, 1.7.0_269 (and specifically the minor/patch(?) version "269").

I did something really similar for another file and used this syntax:

JAVA_VER=$("/the/directory/to/jversion.sh" | awk -F'java.version:' '{print $2}' | xargs )

Which works nicely, but for some reason isn't working for this one (and admittedly my knowledge of bash commands is lacking, so I'm not sure how to correctly modify it):

APP_JAVA_VER=$(/the/directory/to/java/bin/./java -version | awk -F'java version' '{print $2}' | xargs)

It just outputs nothing. Could anyone point me in the right direction for how to do this? I'm seeing lots of different approaches to this type of problem but I'd like to actually understand where my code is going wrong and how to modify it. Does it have something to do with it not being a .sh file? Thank you for any help! 英文:

I'm trying to extract a version number from a file with a directory that looks like this:

... /directory/to/java/bin/./java -version

Running that from the command line produces this output:

java version "1.7.0_269"
Java(TM) SE Runtime Environment (build 7.0.8.10 - pkshd34234hsd284842_01(xop4_))
bla bla bla
whole bunch of 
extra info

And I need to extract just that version number, 1.7.0_269 (and specifically the minor/patch(?) version "269").

I did something really similar for another file and used this syntax:

JAVA_VER=$("/the/directory/to/jversion.sh" | awk -F'java.version:' '{print $2}' | xargs )

Which works nicely, but for some reason isn't working for this one (and admittedly my knowledge of bash commands is lacking, so I'm not sure how to correctly modify it):

APP_JAVA_VER=$(/the/directory/to/java/bin/./java -version | awk -F'java version' '{print $2}' | xargs)

It just outputs nothing. Could anyone point me in the right direction for how to do this? I'm seeing lots of different approaches to this type of problem but I'd like to actually understand where my code is going wrong and how to modify it. Does it have something to do with it not being a .sh file? Thank you for any help!

答案1 {#1}

得分: 2

你的问题是 `java -version` 输出到 STDERR,而 awk 在 STDIN 上查找。

尝试这样做:

APP_JAVA_VER=$(/the/directory/to/java/bin/./java -version 2>&1 | awk -F'java version' '{print $2}' | xargs)

英文:

Your problem is that java -version outputs to STDERR, and awk looks on STDIN.

Try this:

APP_JAVA_VER=$(/the/directory/to/java/bin/./java -version 2>&1 | awk -F'java version' '{print $2}' | xargs)

赞(2)
未经允许不得转载:工具盒子 » 使用Bash shell脚本从文件中解析版本号。