Call function from Find command

I am writing a bash script in which I am using the find command to find files, and then I'd like to send the output to a function (in the same script) that will process those results (such as test if the file is an image, and other things).

However, try as I might, I can't get the function to call properly. The best I have is :

#Determine File Types.
fltyp() {
	echo $1
}

#Find files and then send to fltyp function.
for i in `echo $idir | sed 's/,/ /g'`
	do
		find -type f -exec bash -c 'fltyp {}' \;
	done

Which outputs a bash: fltyp: command not found.

Any advice/ideas?

Hi,

to have a personal function in bash you can try this:
write a function in a file, i.e. MYFUNC.txt, and in a shell type

. MYFUNCT.txt

.
Note a space after the dot.

The kind of file you can view in this way:

find /everywhere/ -type f -exec file {} \; 

Hi,
I don't think there is something like fltyp in find and why don't you try searching for a particular filetype first, then store it in a variable and then pass that variable to desired function which gives the output as desired.

@pokerino : I was hoping to avoid having multiple files for this script. Is there no way to have the find command call a function that lies within the same .sh script?

#mayursingru : I am searching for particular file types using the find and file commands (as well as some other processing, hence the function).

So, I tried :

#Determine File Types.
fltyp() {
	echo 'This worked'
}

#Find files and then send to fltyp function.
for i in `echo $idir | sed 's/,/ /g'`
	do
		find $i -type f | fltyp
	done

I do not get an error message, but I don't get the `echo 'This worked' either.

A function is a function within your current shell. We don't need to start another bash because we can just refer to the function like it is a command.
Guessing the $idir contains a valid comma-separated list of directories, we can rearrange the script to avoid using "for" when we have directory or file names.

This construct works for any number of files and for filenames containing space characters:

#Determine File Types.
fltyp()
{
	echo "$1"
}

#Find files and then send to fltyp function.
echo "${idir}" | tr ',' '\n' | while read directory
do
       find "${directory}" -type f -print | while read filename
       do
               # Call Function fltyp with the filename as a parameter
               fltyp "${filename}"
       done
done

@methyl

That appears to be exactly what I was looking for. The while read is a new trick to me, thank you.

'while read' is very useful and IMHO should be taught well before backticks are. It solves some of the same problems in a safer way...