LinuxQuestions.org
Register a domain and help support LQ
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-10-2009, 10:02 AM   #1
GerardM
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Rep: Reputation: 0
Why does this not work when I want to have the result in a variable ?


The command svn info | grep Revision | awk '{print $2}' works fine, I get a number.

When i do

Revision=svn info | grep Revision | awk '{print $2}'

it fails and I get
info: Writing node (dir)Top...
info: Done.
As a reply..

What am I doing wrong ?
Thanks,
GerardM
 
Old 06-10-2009, 10:05 AM   #2
JulianTosh
Member
 
Registered: Sep 2007
Location: Las Vegas, NV
Distribution: Fedora / CentOS
Posts: 674
Blog Entries: 3

Rep: Reputation: 90
try
Revision=$(svn info | grep Revision | awk'{print $2}')
 
Old 06-10-2009, 10:46 AM   #3
kpraveen455
Member
 
Registered: Feb 2009
Location: Hyderabad
Distribution: fedora
Posts: 33

Rep: Reputation: 17
Smile

Hi GerardM,

In Bash, everything u assign to a variable is a string...

Revision=svn info | grep Revision | awk '{print $2}'

From the above scenario, 'svn' is treated as a string and will be assigned to Variable 'Revision'. The command 'info', here will be treated as a command, and 'info' is a linux command which reads Info documents. And since (in this case) no arguments are there for 'info' command, it gives following error:

info: Writing node (dir)Top...
info: Done.


Secondly, $(... your exp) will give the final result. And this result can be directly assigned to the variable 'Revision'

Note: assignment of a string(separated by spaces) to a variable should be enclosed in quotes.
 
Old 06-10-2009, 11:03 AM   #4
GerardM
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
testing

Hoi,
Revision=$(svn info | grep Revision | awk'{print $2}')
gives me -bash: awk{print $2}: command not found

Revision==`svn info | grep Revision | awk '{print $2}'` works for me

Thanks,
GerardM
 
Old 06-10-2009, 11:31 AM   #5
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by GerardM View Post
Hoi,
Revision=$(svn info | grep Revision | awk'{print $2}')
gives me -bash: awk{print $2}: command not found

Revision==`svn info | grep Revision | awk '{print $2}'` works for me

Thanks,
GerardM
Try Revision=$(svn info | grep Revision | awk '{print $2}') The blank after awk is a required separator.

<edit>
Also Revision=$(svn info | awk '/Revision/{print $2}') might work negligibly faster, but it looks more efficient.
</edit>

Last edited by PTrenholme; 06-10-2009 at 04:09 PM.
 
Old 06-10-2009, 11:34 AM   #6
jamescondron
Member
 
Registered: Jul 2007
Location: Scunthorpe, UK
Distribution: Ubuntu 8.10; Gentoo; Debian Lenny
Posts: 961

Rep: Reputation: 69
Quote:
Originally Posted by GerardM View Post
Revision==`svn info | grep Revision | awk '{print $2}'` works for me
Thats because that is a comparison
 
Old 06-10-2009, 04:13 PM   #7
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,186

Rep: Reputation: 346Reputation: 346Reputation: 346Reputation: 346
Quote:
Originally Posted by jamescondron View Post
Thats because that is a comparison
Yes, as he wrote it. But the backquotes will, in most shells, return the same string as $(...). I assumed the OP had just held down the = key a little too long.
 
Old 06-11-2009, 12:27 AM   #8
GerardM
LQ Newbie
 
Registered: Oct 2006
Posts: 6

Original Poster
Rep: Reputation: 0
Oops, indeed only one = indeed.
Thanks,
GerardM
 
Old 06-11-2009, 01:05 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by GerardM View Post
Oops, indeed only one = indeed.
Thanks,
GerardM
no need grep
Code:
Revision=$(svn info | awk '/Revision/{print $2}')
 
  


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
How to pass a result of exec command in perl to a variable??? HyperTrey Programming 3 05-23-2008 01:47 PM
Grep result passed to variable cornish Programming 5 11-15-2007 06:05 PM
bash script: how to assign an output (not the result) to a variable? Singing Banzo Programming 8 10-01-2006 07:29 PM
cannot export result from awk into a variable in a bash script Emmanuel_uk Linux - Newbie 4 03-07-2005 02:54 AM
multiline grep result > multiple variable? mikshaw Linux - Software 5 12-25-2003 11:13 PM


All times are GMT -5. The time now is 08:48 PM.

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