LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 06-15-2011, 01:50 PM   #1
Francoo
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Rep: Reputation: Disabled
Bash function for searching files problem.


Hey,
I'm new here, but I know this forum long time ago, so I decided register.

I'm making a small script for searching and doing some operations with photos, but I'm kinda stuck on this little function:

Code:
function findallformat {
prefix=""
if [ $1 = -pre ] then
	shift
	prefix=${1}
	shift
fi
if [ -z $1 ]; then
	find -name ${prefix}*
else for i in $@
	do find -name ${prefix}*${1}
done
}
As you can see, I'm a newbie on Bash, and I'm just starting to program. That function should find for every file with a certain type; and you can specify a prefix using a "-pre" followed by the prefix that you want to search. The format should be "stackable", so you can use as many types that you want, without repeating the same function on the code.

Example: findallformat -pre IMG_ .JPG .CR2 #That should search files that start with "IMG_" and finishes with .JPG and .CR2.

My problem it's that, when I try to use it on the script, it says "bash: syntax error near `token' unexpected `}'"

So, can someone help me? I know it's a silly question, but I'm really stuck with it, and I tried almost everything for fixing it.
Also, what could I do for making the function nicer? Any suggestion? Tips would be appreciated.

(sorry for my bad english, I'm Brazilian)

Last edited by Francoo; 06-15-2011 at 02:03 PM.
 
Old 06-15-2011, 02:22 PM   #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
Here is a corrected version of your function (see text in red)
Code:
function findallformat {
prefix=""
if [ $1 = -pre ]; then
	shift
	prefix=${1}
	shift
fi
if [ -z $1 ]; then
	find -name ${prefix}\*
else for i in $@
	do find -name ${prefix}\*${i}
done
fi
}
 
1 members found this post helpful.
Old 06-15-2011, 02:24 PM   #3
Francoo
LQ Newbie
 
Registered: Jun 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
Ops that was kinda of a #Fail
Thanks for the help, it worked perfectly.

Last edited by Francoo; 06-15-2011 at 02:26 PM.
 
Old 06-15-2011, 02:26 PM   #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
You're welcome!
 
Old 06-16-2011, 01:03 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,005

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Please mark as SOLVED once you have a solution.
 
  


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
Problem Searching for php files with Find and Grep ssmela Linux - General 3 11-02-2010 01:55 PM
Bash Function Problem Gary Baker Linux - Software 1 10-29-2010 07:05 PM
[SOLVED] Bash Function Problem Gary Baker Linux - Software 3 10-26-2010 02:02 PM
[SOLVED] Problem with getting array from function (bash) DrLove73 Programming 19 09-25-2009 07:07 AM
bash searching directries and find the files bekirsagir Programming 1 08-19-2005 04:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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