how to import linux command in shell script file as the value of a variable ?

I am using Fedora 10. If I write java -version in terminal, it gives me java version. I want to write a shell script file to check whether the java version is greater than 1.5. The main problem is that I donot know how to assign the value of java -version command as to a variable of shell script. How to create shell script file for that ?

Answers


Use backticks:

JAVA_VERSION=`java-version`

As answered above, you can use backticks or (if your keyboard doesn't have them) use the following format:

JAVA_VERSION=$(command)


you should direct stderr to stdout

$ var=$(java -version 2>&1)
$ set -- $var
$ echo $3
"1.6.0_0"
$ echo ${3//[._\"]/}
1600

Now you can compare $ver to see if its greater than 1500.


All the above Answer will work for you. below are the common example which you can use to get the value of variable:

JAVA_VERSION=`java-version` or JAVA_VERSION="java-version" or JAVA_VERSION=$(command)

The way is to set the value of variable is many. You can use your easiest way to fulfill your requirement. Like I'm using

var=$(awk -F'"' '/^variable=/ {print $2}' script1.sh )
echo $var

Need Your Help

Python: AttributeError: type object 'Ui_Form' has no attribute 'comboBox'

python

I have two modules test.py and fill_combobox.py. The fill_combobox is for a default values in the combobox. There is still a problem with communication between modules.