Register
It is currently Fri Oct 24, 2014 6:45 am

How can I received command output and store it to a variable


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Apr 03, 2010 7:20 pm   

Joined: Sat Apr 03, 2010 7:13 pm
Posts: 2
Suppose I want to account number of files beginning with abc , I can use "ls 'abc* | grep abc | wc -l", this will return me a number.
I want to store this number in a variable, say var1, so I tried
1. "ls 'abc* | grep abc | wc -l |read var1", but this didn't work as var1 has no value somehow.
2. var1='ls 'abc* | grep abc | wc -l', this just assign the entire string "ls 'abc* | grep abc | wc -l" to var1, which is not I wanted.

I don't want to store the value to a temporary file and then read the value from that file. I think there should be a direct way to get the value, but don't know how. I know in tcsh, one can just use set var1='ls 'abc* | grep abc | wc -l', but it also doesn't work in bash. Can anyone give any clue about this? Thanks!


Top
 Profile  
 PostPosted: Sat Apr 03, 2010 8:11 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 550
Hi athlonshi,

in bash, is prefered this form:
Code:
var1=$(commands)
check bash's man page for 'Command Substitution'


Top
 Profile  
 PostPosted: Sun Apr 04, 2010 7:40 am   

Joined: Sat Apr 03, 2010 7:13 pm
Posts: 2
Thanks very much!


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP