Call function from Find command | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Call function from Find command

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-19-2012
twjolson twjolson is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 January 2014, 9:38 AM EST
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
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 :


Code:
#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?
Sponsored Links
    #2  
Old 04-19-2012
pokerino pokerino is offline
Registered User
 
Join Date: Apr 2012
Last Activity: 8 July 2012, 7:16 AM EDT
Posts: 36
Thanks: 0
Thanked 5 Times in 5 Posts
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
Code:
. MYFUNCT.txt

.
Note a space after the dot.

The kind of file you can view in this way:

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

Sponsored Links
    #3  
Old 04-19-2012
mayursingru's Avatar
mayursingru mayursingru is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 9 February 2014, 12:12 AM EST
Location: Pune
Posts: 39
Thanks: 1
Thanked 5 Times in 5 Posts
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.

Last edited by methyl; 04-19-2012 at 01:09 PM.. Reason: fix in-ine code tag
    #4  
Old 04-19-2012
twjolson twjolson is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 January 2014, 9:38 AM EST
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
@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 :


Code:
#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.

Last edited by twjolson; 04-19-2012 at 01:14 PM..
Sponsored Links
    #5  
Old 04-19-2012
methyl methyl is offline Forum Advisor  
Advisor
 
Join Date: Mar 2008
Last Activity: 18 April 2014, 5:13 AM EDT
Posts: 6,396
Thanks: 287
Thanked 673 Times in 642 Posts
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:


Code:
#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


Last edited by methyl; 04-19-2012 at 01:26 PM.. Reason: schoolboy error in input stream
Sponsored Links
    #6  
Old 04-19-2012
twjolson twjolson is offline
Registered User
 
Join Date: Dec 2011
Last Activity: 10 January 2014, 9:38 AM EST
Posts: 29
Thanks: 0
Thanked 1 Time in 1 Post
@methyl

That appears to be exactly what I was looking for. The while read is a new trick to me, thank you.
Sponsored Links
    #7  
Old 04-19-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 October 2014, 4:38 PM EDT
Location: Saskatchewan
Posts: 19,683
Thanks: 823
Thanked 3,352 Times in 3,139 Posts
'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...
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
After exit from function it should not call other function rohit22hamirpur Shell Programming and Scripting 1 10-13-2011 09:57 AM
use internal function with find command victorcheung Shell Programming and Scripting 1 08-12-2010 03:47 AM
diffrence between method call and function call in perl Zaxon Infrastructure Monitoring 4 03-30-2009 02:28 PM
Function Call Zaxon Shell Programming and Scripting 4 03-08-2009 05:39 AM
call function Jamil Qadir Shell Programming and Scripting 4 03-20-2007 05:07 AM



All times are GMT -4. The time now is 11:16 AM.