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 02-19-2009, 10:31 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
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
 
Old 02-19-2009, 10:35 AM   #2
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
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
 
Old 02-19-2009, 10:36 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

instead of
CLUSTERS=$(ls cluster_*)

You used double quotes instead of backticks or the $(...) form.
 
Old 02-19-2009, 10:41 AM   #4
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Thank you thank you thank you!

So what is the difference between " " , ' ' , and ` ` in bash? When should each be used?
 
Old 02-19-2009, 10:49 AM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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

Last edited by schneidz; 02-19-2009 at 10:50 AM.
 
Old 02-19-2009, 10:49 AM   #6
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

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

instead of
CLUSTERS=$(ls cluster_*)

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


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
BASH: Read entire file line in for loop clinton Programming 16 04-18-2013 12:06 PM
BASH: Reading long filenames into an array using a loop DaneM Programming 12 09-11-2009 07:24 AM
bash - loop over variable array names talanis Programming 2 02-19-2009 11:09 AM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration