开发者

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

开发者 https://www.devze.com 2022-12-12 11:40 出处:网络
I am using Fedora 10. If I write java -version in terminal, it g开发者_开发技巧ives me java version. I want to write a shell script file to check whether the java version is greater than 1.5.

I am using Fedora 10. If I write java -version in terminal, it g开发者_开发技巧ives 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 ?


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

JAVA_VERSION=$(command)


Use backticks:

JAVA_VERSION=`java-version`


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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号