LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 04-03-2010, 08:26 PM   #1
athlonshi
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Rep: Reputation: 0
Question How can I receive command output and store it to a variable


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!
 
Old 04-03-2010, 08:52 PM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
You've got the right idea, just a different shell, so a differnet syntax:

Code:
#!/bin/bash

var1=$(ls abc* | grep abc | wc -l)
Now, before you go any further: there's extra logic in that above statement; as in, the grep is pointless. Also, this will list directories containing "abc" as well.

I would rather do this:
Code:
var=$(find . -type f -maxdepth 1 -iname "abc*" | wc -l)
That will identify all FILES in the working directory, which begin with abc. Read the man page for the `find` command to understand how it works.

Any questions, feel free to ask

Sasha

Last edited by GrapefruiTgirl; 04-03-2010 at 08:54 PM. Reason: missing a quote
 
Old 04-03-2010, 10:17 PM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
You may be better served by a loop like:
Code:
while IFS="" read -r -d "" file ; do
   declare length="$(wc -l "$file")"
   length="${length%% *}"
   ...
done < <(find . -type f -maxdepth 1 -iname "abc*" -print0)
This correctly handles filenames that have spaces and filenames.
 
Old 04-04-2010, 01:21 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 239Reputation: 239Reputation: 239
Code:
for file in abc*;do ((i++)) ;done
echo $i
 
Old 04-04-2010, 01:25 AM   #5
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542Reputation: 542
@ ghostdog -- gotta like that elegant simplicity! Nice one.

Sasha
 
Old 04-04-2010, 08:09 AM   #6
athlonshi
LQ Newbie
 
Registered: Apr 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Smile

It works! Thanks, Sasha and other folks!
Yu

Quote:
Originally Posted by GrapefruiTgirl View Post
You've got the right idea, just a different shell, so a differnet syntax:

Code:
#!/bin/bash

var1=$(ls abc* | grep abc | wc -l)
Now, before you go any further: there's extra logic in that above statement; as in, the grep is pointless. Also, this will list directories containing "abc" as well.

I would rather do this:
Code:
var=$(find . -type f -maxdepth 1 -iname "abc*" | wc -l)
That will identify all FILES in the working directory, which begin with abc. Read the man page for the `find` command to understand how it works.

Any questions, feel free to ask

Sasha
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
i need to store the output of bitset() in a variable anurupr Linux - Newbie 1 03-05-2010 12:32 AM
how to store a command in a variable lemon09 Programming 11 02-01-2010 02:03 AM
Using variable to store command lines in a pipeline? zizou86 Programming 6 01-29-2010 10:20 AM
How to store SQL output in a variable atulsh General 1 09-18-2008 01:22 PM
Bash store last line from displayed text output in a variable carl0ski Programming 1 01-16-2007 03:38 AM


All times are GMT -5. The time now is 08:01 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