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

huangapple go评论134阅读模式
英文:

Parsing version number from file using shell script in Bash

问题

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

得分: 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)

huangapple
  • 本文由 发表于 2023年8月11日 01:26:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76878032.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定