PDA

View Full Version : I need the output of command in a script variable


turtle
2016-04-03, 21:19
While this may seem like a really dumb question I just can't make it work. Specifically for "java -version".

I'm trying to add the result to a variable that gets echoed into a log so I know what java version was used. However every way I've tried to make it happen it executes the version command on the screen and not into the log, even when setting the variable.

So I would think something like:
# export DATE=`date`; echo $DATE
Sun Apr 3 22:16:21 EDT 2016
#

but for the java version:
# export JAVA_VERSION=`java -version`
# echo $JAVA_VERSION
java version "1.8.0_77"
...
#

Something like that, but no it just doesn't seem to work in terminal on my OS 10.9 machine. So please help me figure this out if you can. I don't really want to write a script to just to do this and then call that script to set the variable if I can help it. Of course, I'm not even sure that's an option with how java is working.

AsLan^
2016-04-04, 09:41
I think the Java version is outputting on the error stream.

Try:

java -version 2>> log.txt

Does that help?

Brad
2016-04-04, 14:15
AsLan is correct! java -version is dumping its output to the stderr stream.

Here's a working solution that captures only the version string:

bradsmith@Hackintosh:~$ JAVA_VERSION=$(java -version 2>&1 | head -n 1 | sed -e 's/.*"\(.*\)".*/\1/')
bradsmith@Hackintosh:~$ echo $JAVA_VERSION
1.8.0_60


"2>&1" tells the shell to redirect the stderr to the stdout stream.

"head -n 1" says we only want to look at the first line of output and discard the rest.

The sed regex says to capture the contents between the double quotes and output only that capture group, discarding the rest.

Brad
2016-04-04, 14:23
Also, if you're on a Mac (not sure how standard this is elsewhere) you can use another tool called "java_home" to show the current default java home as well as list other available java homes.

Here's some example usage:

bradsmith@Hackintosh:~$ /usr/libexec/java_home
/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home

bradsmith@Hackintosh:~$ /usr/libexec/java_home -V
Matching Java Virtual Machines (4):
1.8.0_60, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home
1.8.0_51, x86_64: "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home
1.6.0_65-b14-466.1, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-466.1, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home

bradsmith@Hackintosh:~$ /usr/libexec/java_home --help
Usage: java_home [options...]
Returns the path to a Java home directory from the current user's settings.

Options:
[-v/--version <version>] Filter Java versions in the "JVMVersion" form 1.X(+ or *).
[-a/--arch <architecture>] Filter JVMs matching architecture (i386, x86_64, etc).
[-d/--datamodel <datamodel>] Filter JVMs capable of -d32 or -d64
[-t/--task <task>] Use the JVM list for a specific task (Applets, WebStart, BundledApp, JNI, or CommandLine)
[-F/--failfast] Fail when filters return no JVMs, do not continue with default.
[ --exec <command> ...] Execute the $JAVA_HOME/bin/<command> with the remaining arguments.
[-R/--request] Request installation of a Java Runtime if not installed.
[-X/--xml] Print full JVM list and additional data as XML plist.
[-V/--verbose] Print full JVM list with architectures.
[-h/--help] This usage information.

turtle
2016-04-04, 17:48
That worked perfectly guys. Thank you very much for the help with this. Now I'm going to be able to narrow down why a cron is not working right.

turtle
2016-04-06, 22:13
So how do I set java to choose my 1.8 install? Beast is having an issue initializing the display because the 10.9 hackintosh doesn't use 1.8. If I'm in Terminal and initiate the script it works perfectly.


$ /usr/libexec/java_home -V
Matching Java Virtual Machines (0):

Default Java Virtual Machines (2):
1.6.0_65-b14-462, x86_64: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home
1.6.0_65-b14-462, i386: "Java SE 6" /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home

/System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home


From my command script it sees the right version:

Java Version info
whoami, turtle2472
Java Version: 1.8.0_77
Java Home: /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home

Brad
2016-04-07, 06:44
Ah, this is a common mistake, and one that Oracle does a terrible job explaining. It looks like you've downloaded the Java 8 browser plugin (which rarely actually even works) but not the Java 8 JDK/JRE. Technically the plugin does include some of the JRE tools, but it unsurprising that it doesn't appear with a proper "Java home" for a lot of Terminal operations. If you want fully-functioning command-line Java tools, you need the JDK/JRE. Try installing this:

http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html


edit: Definitions! JRE = Java runtime environment. JDK = Java development kit. Hypothetically, you only need a JRE, but Oracle is stupid and you really need the JDK to do anything useful. :)

turtle
2016-04-07, 11:46
:no:

I should have known. I didn't even notice the difference with "dk" and "re". I was spoiled by it being bundled in Mac OS. That and I hate java typically because it's so resource hungry.

This seems to have fixed it though. I'm running a test run in a screen session and it's working now where it wasn't before. I'll know for sure once the cron hits at 3pm.

Thanks for the reminder.