LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   For loop in bash using ls to get array of file names (https://www.linuxquestions.org/questions/linux-newbie-8/for-loop-in-bash-using-ls-to-get-array-of-file-names-705931/)

bioinformatics_guy 02-19-2009 10:31 AM

For loop in bash using ls to get array of file names
 
So I have a group of files of the form:

cluster_1 cluster_2 cluster_3 etc


CLUSTERS="ls cluster_*"

for zzCLUSTER in $CLUSTERS
do
cat "${zzCLUSTERS}" | tr '/' ' ' | uniq | cat > "${zzCLUSTERS}".reads
done

but for some reason this isent working? I've looked at like 50 examples and I can't seem to get it to work. It says that I can't redirect

bioinformatics_guy 02-19-2009 10:35 AM

Correction, this is the code I have essentially

clusterlist="ls cluster_*"

for zzCLUSTERS in "$clusterlist"
do
cat "${zzCLUSTERS}" | tr '/' ' '| cat > "${zzCLUSTERS}".reads
done

the error I'm getting is

cat: ls cluster_*: No such file or directory

jschiwal 02-19-2009 10:36 AM

You can use:
for zzCLUSTER in cluster_*; do
...
done

instead of
CLUSTERS=$(ls cluster_*)

You used double quotes instead of backticks or the $(...) form.

bioinformatics_guy 02-19-2009 10:41 AM

Thank you thank you thank you!

So what is the difference between " " , ' ' , and ` ` in bash? When should each be used?

schneidz 02-19-2009 10:49 AM

Code:

schneidz@lq /sp> ls temp
hello/        hello-c.s    hello-cc.s    hello-cpp.C  hello-cpp.x*
hello-c.C    hello-c.x*    hello-cc.x*  hello-cpp.s
schneidz@lq /sp> dir=temp
schneidz@lq /sp> echo "ls $dir"
ls temp
schneidz@lq /sp> echo 'ls $dir'
ls $dir
schneidz@lq /sp> echo `ls $dir`
hello/ hello-c.s hello-cc.s hello-cpp.C hello-cpp.x* hello-c.C hello-c.x* hello-cc.x* hello-cpp.s


jschiwal 02-19-2009 10:49 AM

You can use:
for zzCLUSTER in cluster_*; do
...
done

instead of
CLUSTERS=$(ls cluster_*)

You used double quotes instead of backticks or the $(...) form.


All times are GMT -5. The time now is 04:39 PM.