LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 04-16-2009, 07:50 AM   #1
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Rep: Reputation: 15
Bash test if pattern match of file exists


So I have an if loop which iterates through all files of form cluster_*

The problem is, if there are not files that match that pattern, my script trips up. What I'd like to do is, test whether any files match the pattern cluster_* and if so, continue.

What I was thinking is:

if [ -f cluster_* ] ; then
...
...
fi

but since it does not recognize cluster_* as a pattern, it searches exactly cluster_* . Any suggestions?
 
Old 04-16-2009, 07:59 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Maybe I don't get the exact problem. Are you trying to see if there are files whose name begins with "cluster_" inside specific directories? Why don't you use ls, if this is the case?
Code:
if ls cluster_* > /dev/null 2>&1
then
  echo cluster files found
else
  echo cluster files not found
fi
 
Old 04-16-2009, 09:16 AM   #3
bioinformatics_guy
Member
 
Registered: Aug 2008
Posts: 54

Original Poster
Rep: Reputation: 15
Maybe I don't get the exact problem. Are you trying to see if there are files whose name begins with "cluster_" inside specific directories? Why don't you use ls, if this is the case?

Yes, downstream in my script, I use the files in a for loop:

for zzCLUSTERS in cluster_*; do

cat "${zzCLUSTERS}" | tr '/' ' ' | awk '{print $1}' | sort | uniq | cat > reads."${zzCLUSTERS}"

done

But if there are no matches for cluster_*, it enters cluster_\* into ${zzCLUSTERS} , which throws off my script which is why I wanted this test.

Would you mind explaining the syntax to me?

if ls cluster_* > /dev/null 2>&1

I'm use to seeing if statements as

if [ ] ;

and > as a redirect

what does /dev/null mean and what does

2>&1 mean?
 
Old 04-16-2009, 11:17 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Ok. The if statement can be used to test an expression enclosed in square bracket or to test the result of a command, as in my example. You know about the exit code of a command? Any command in linux has an exit code, which is usually 0 if the command had success, a value different than 0 (in most cases 1) if the command failed.

You can retrieve the exit code explicitly using
Code:
$ command
$ echo $?
where $? is a special variable storing the exit code of the previous command.

The if ls statement test the result of the ls command based on its exit code: if it is successful the commands inside the if/then block are executed, otherwise they are skipped. The
Code:
> /dev/null 2>&1
just redirect the standard output to /dev/null, which is a sort of black hole in unix systems. This prevent the output to be shown in the terminal, since you're not really intersted to visualize the list of files. The 2>&1 just redirect the standard error to the standard output, so that even the error message (when no file is found) is not displayed. Hope it is clear now.
 
Old 04-17-2009, 06:42 AM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by bioinformatics_guy View Post
So I have an if loop which iterates through all files of form cluster_*

The problem is, if there are not files that match that pattern, my script trips up. What I'd like to do is, test whether any files match the pattern cluster_* and if so, continue.

What I was thinking is:

if [ -f cluster_* ] ; then
...
...
fi

but since it does not recognize cluster_* as a pattern, it searches exactly cluster_* . Any suggestions?
Code:
shopt nullglob
In this case filename expansion for noon-exist filenames will be empty string
 
  


Reply



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
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
How to delete the file using pattern match? nishanthhampali Programming 3 04-16-2008 12:50 PM
to test that a file exists but is not writeable? johnpaulodonnell Linux - Newbie 2 01-30-2007 06:11 AM
how to use the sed w option to redirect pattern match to file nickleus Linux - General 11 04-18-2006 08:34 AM
if test for a file name pattern Melsync Programming 12 12-15-2005 05:42 PM

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

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