LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Shell Scripting troubles (https://www.linuxquestions.org/questions/linux-newbie-8/shell-scripting-troubles-459694/)

Reegz 06-30-2006 04:30 AM

Shell Scripting troubles
 
Hi guys,

Feel really stupid asking this.

I'm trying to write a script which will swap the symbolic links for my different java versions.

What I'm trying to do is pipe the output of "java -version" to grep and grep for either 1.4 or 1.5 and then set the sym links accordingly.

Wasn't having luck with that so decided to pipe to file, then cat the file and check for the version number there.

But so far i'm having trouble piping the output of "java -version" to anything!

Some pointers please.

Tinkster 06-30-2006 04:41 AM

java -version 2>&1|grep version | egrep -o "1.[45]"


Cheers,
Tink

Reegz 06-30-2006 04:54 AM

Dude!!! that rocks!!

thanks man

could you please explain it to me

timmeke 06-30-2006 08:42 AM

The grep selects the line (out of the java -version output) that contains the text "version".
egrep is the same as grep -E and simply grep's based on regular expressions (see man regex)
-o makes (e)grep print only the matching part, so the part that matches "1.4" or "1.5".

The regular expression "1.[45]" matches both "1.4" and "1.5".

reshojaei 06-30-2006 09:35 AM

I didnt understand 2>&1
??? could you plz explain

binary_y2k2 06-30-2006 10:21 AM

That redirect the output from stderr to stdout
take a look at http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html

reshojaei 06-30-2006 01:10 PM

as I know 2>&1 will send stderr to stdout.
i dont know why the result of java -version should go to stderr?

Tinkster 06-30-2006 03:20 PM

I don't know either, but it sure does for my version ;}


Cheers,
Tink

binary_y2k2 06-30-2006 04:50 PM

Mine (sun-java5-jre in ubuntu) prints -version to stdout not stderr so that's just strange, maybe I have different version
Code:

java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


Reegz 07-01-2006 01:49 AM

Quote:

Originally Posted by binary_y2k2
Mine (sun-java5-jre in ubuntu) prints -version to stdout not stderr so that's just strange, maybe I have different version
Code:

java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)



Well that was what i also could not understand. initially, i was trying to pipe the output to a file, and quite clearly that didnt work.

Tinkster 07-01-2006 02:43 AM

Quote:

Originally Posted by binary_y2k2
Mine (sun-java5-jre in ubuntu) prints -version to stdout not stderr so that's just strange, maybe I have different version
Code:

java -version
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)


How did you tell the difference between stderr & stdout in that
invocation?

On my wifes ubuntu-rig (with an older JRE) we see the following:
Code:

[tink@beast:~]$ java -version 2>&1|grep version | egrep -o "1.[45]"
1.4
[tink@beast:~]$ java -version |grep version | egrep -o "1.[45]"
java version "1.4.2-02"
Java(TM) 2 Runtime Environment, Standard Edition (build Blackdown-1.4.2-02)
Java HotSpot(TM) 64-Bit Server VM (build Blackdown-1.4.2-02, mixed mode)

Which to me indicates that version for that version prints to stderr.


Cheers,
Tink

binary_y2k2 07-01-2006 09:14 AM

Code:

user@host:~$ java -version|grep version
java version "1.5.0_06"

Maybe I have some weird config in java, although I did compile from source so maybe I did something in there, I can't remember :p

jschiwal 07-01-2006 09:50 AM

I think it makes sense to use stderr for diagnostic messages. Suppose that the program follows the filter model. It takes input on stdin, processes it, and sends the output to stdout. You can redirect the processed output to a file, and still use the --verbose mode to display information on what is going on.

"gcc -v" works the same as your java example.

jschiwal@hpamd64:~> java -version 2>&1 | grep -o 'version.*$'
version "1.5.0_07"
jschiwal@hpamd64:~> gcc -v 2>&1 | grep -o 'version.*$'
version 4.1.0 (SUSE Linux)


All times are GMT -5. The time now is 05:12 AM.