LinuxQuestions.org
Visit Jeremy's Blog.
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 10-30-2007, 09:45 AM   #1
christianunix
Member
 
Registered: Oct 2007
Posts: 40

Rep: Reputation: 15
another simple unix scripting question! storing values in variables


how can I store the results of commands, for example if wc -l returns 6, how can I store the value into a variable in a unix script? thanks LQ!!
 
Old 10-30-2007, 09:55 AM   #2
m0rg
Member
 
Registered: Jun 2003
Location: France
Distribution: slackware 11.0
Posts: 80

Rep: Reputation: 15
Try backquotes, e.g.
RESULT=`cat my_file.txt | wc -l`
 
Old 10-30-2007, 10:04 AM   #3
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
or use the $() construct:
Code:
RESULT=$(wc -l my_file.txt)
I find this more readable and easier to debug.
Quote:
Originally Posted by m0rg
RESULT=`cat my_file.txt | wc -l`
That's a UUOC - wc will take a file as input, no need to spawn a separate instance of cat
 
Old 10-30-2007, 12:54 PM   #4
christianunix
Member
 
Registered: Oct 2007
Posts: 40

Original Poster
Rep: Reputation: 15
I do appreciate both of you for your kind response, and pwc is the winner this time!! pwc's method worked.

m0rg's method stores "cat file|wc -l" the command into the variable. The content of the variable becomes the "cat file|wc -l" not the result of the command.

Also,

Code:
result=$(cat file|wc -l)
is better than
Code:
result=$(wc -l file)
cat file|wc -l will save 7 into the variable, and wc -l file will save "7 file" into the variable

Last edited by christianunix; 10-30-2007 at 12:58 PM.
 
Old 10-30-2007, 01:13 PM   #5
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,846

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by christianunix View Post
Also,

Code:
result=$(cat file|wc -l)
is better than
Code:
result=$(wc -l file)
cat file|wc -l will save 7 into the variable, and wc -l file will save "7 file" into the variable
In which case, to avoid using a cat command (which can take a very long time on its own), it's better to use the < redirect, which has the parallel advantages of avoiding the filename becoming part of the output and the unnecessary use of cat:
Code:
RESULT=$(wc -l < file)
Also, make sure you use backticks (on my keyboard it's the key above the tab key) _not_ apostrophes or quotes. Backticks have exactly the same effect as $().
 
  


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
UNIX scripting question christianunix Linux - Software 2 10-07-2007 12:38 PM
simple scripting question jerf Linux - Newbie 4 09-19-2007 03:12 AM
Simple Shell Scripting Question hellomynameisphil Programming 2 08-27-2005 03:41 AM
simple scripting question wedgeworth Programming 6 02-04-2004 10:30 AM
storing multiple values within one field in mysql antken Programming 8 12-15-2002 10:08 PM


All times are GMT -5. The time now is 10:27 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration