LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2006, 05:30 AM   #1
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Rep: Reputation: 15
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.
 
Old 06-30-2006, 05:41 AM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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


Cheers,
Tink
 
Old 06-30-2006, 05:54 AM   #3
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
Dude!!! that rocks!!

thanks man

could you please explain it to me

Last edited by Reegz; 06-30-2006 at 05:55 AM.
 
Old 06-30-2006, 09:42 AM   #4
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
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".
 
Old 06-30-2006, 10:35 AM   #5
reshojaei
LQ Newbie
 
Registered: Oct 2005
Location: toronto
Posts: 8

Rep: Reputation: 0
I didnt understand 2>&1
??? could you plz explain
 
Old 06-30-2006, 11:21 AM   #6
binary_y2k2
Member
 
Registered: Jul 2005
Location: England, UK
Distribution: Ubuntu 8.04 Server, Kubuntu 12.04
Posts: 698
Blog Entries: 1

Rep: Reputation: 31
That redirect the output from stderr to stdout
take a look at http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html
 
Old 06-30-2006, 02:10 PM   #7
reshojaei
LQ Newbie
 
Registered: Oct 2005
Location: toronto
Posts: 8

Rep: Reputation: 0
as I know 2>&1 will send stderr to stdout.
i dont know why the result of java -version should go to stderr?
 
Old 06-30-2006, 04:20 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

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


Cheers,
Tink
 
Old 06-30-2006, 05:50 PM   #9
binary_y2k2
Member
 
Registered: Jul 2005
Location: England, UK
Distribution: Ubuntu 8.04 Server, Kubuntu 12.04
Posts: 698
Blog Entries: 1

Rep: Reputation: 31
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)
 
Old 07-01-2006, 02:49 AM   #10
Reegz
Member
 
Registered: Mar 2006
Location: South Africa
Distribution: Ubuntu, Slackware, openSuse
Posts: 44

Original Poster
Rep: Reputation: 15
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.
 
Old 07-01-2006, 03:43 AM   #11
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 07-01-2006, 10:14 AM   #12
binary_y2k2
Member
 
Registered: Jul 2005
Location: England, UK
Distribution: Ubuntu 8.04 Server, Kubuntu 12.04
Posts: 698
Blog Entries: 1

Rep: Reputation: 31
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
 
Old 07-01-2006, 10:50 AM   #13
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670Reputation: 670
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Scripting: Getting a pid and killing it via a shell script topcat Programming 15 10-28-2007 03:14 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 05:29 AM
BASH Scripting Troubles with IF and FUNCTION semaja2 Linux - Software 10 01-26-2006 03:04 AM
Shell Scripting Troubles!!! sc_3007 Linux - General 3 11-30-2005 02:21 PM
shell interface vs shell scripting? I'm confused jcchenz Linux - Software 1 10-26-2005 04:32 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration