You are not logged in.

#1 2010-02-20 20:09:18

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,676
Website

bash question: capture results of find as a variable (multiple files)

Basically, I need a way to capture the results of a find command to a variable in a bash script so I can recall the filename in a more complex string of commands.  How is this best accomplished?

For example:

$ find /home/user -name '*.wav'

That outputs a list of say 15 .wav files.  I need to feed each filename into a longer string that does several things to the wav files.  If it were just one wav file, I think I could capture it like this:

var=`find /home/user -name '*.wav'`

Then I can simply use ${var} in the longer string of commands.  I dunno how to do it with multiple files in this way.

Last edited by graysky (2010-02-20 20:18:38)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#2 2010-02-20 20:29:36

ZaQ
Member
Registered: 2009-12-12
Posts: 106

Re: bash question: capture results of find as a variable (multiple files)

all='find /home/user -name '*.wav'
for var in $all
do
   echo $var
done

Offline

#3 2010-02-20 20:36:41

Michael C.
Member
From: Vancouver, BC
Registered: 2009-12-16
Posts: 33

Re: bash question: capture results of find as a variable (multiple files)

Apologies, misread.

Last edited by Michael C. (2010-02-20 20:38:31)

Offline

#4 2010-02-20 20:38:36

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,676
Website

Re: bash question: capture results of find as a variable (multiple files)

Yeah, I need to separate each of the wav files as their own variable, run them through a string of commands, then repeat for the 2nd on the the list.


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#5 2010-02-20 20:41:07

Michael C.
Member
From: Vancouver, BC
Registered: 2009-12-16
Posts: 33

Re: bash question: capture results of find as a variable (multiple files)

Oh, I guess I didn't misread.

for foo in $(find /home/user '*.wav')
do
    bar=$(do_something "$foo")
    bigger_var="$bigger_var $bar"
done

Last edited by Michael C. (2010-02-20 20:41:50)

Offline

#6 2010-02-20 20:41:50

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,676
Website

Re: bash question: capture results of find as a variable (multiple files)

Thank you!  This did just what I wanted it to do smile


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

Board footer

Powered by FluxBB