英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论