Is it possible make the shell read functions 1 by 1 and calling an other function?

Greetings,

I m wondering if it's possible do do the following :

I have a simple function called "FindMoveDelete" which does the following :

FindMoveDelete()
{
find . -iname "$FILENAME*.ext" -exec mv {} "$PATH/$VAR" \; &&
find . -maxdepth 1 -type d -iname "$FILENAME*" -exec rm -rf {} \;
}

So basicly it moves the variable "$FILENAME*.ext" to "$PATH/$VAR" and if the move succeeds, deletes the previous of said file directory. (which is basicly the same name)

Now, that's working but what I want to do next is load a configuration file containing a long list of functions. Each functions declaring new variables for $FILENAME $PATH and $VAR as follow :

Function1()
{
FILENAME=function1
PATH="/volume1/my/path/"
VAR=myvar
}

That way when I want to modify or add new functions I simply have to edit my configuration file with all the functions...

Only problem is : How do I make the shell read function 1 then apply the MoveAndDelete then read function 2, apply MoveAndDelete.

I guess i could call the MoveAndDelete function within each function like so :

Function1()
{
FILENAME=function1
PATH="/volume1/my/path/"
VAR=myvar
MoveAndDelete
}

But it looks ugly :smiley:

Thanks for taking the time to read

Instead having 9,999 functions which do the same thing with slightly different values, just have your function take parameters.

Never use PATH as a variable. That's a special variable, changing its value will mess up tons of things.

That code couldn't possibly work. '$FILENAME' in single quotes won't expand. 'FILENAME*' won't expand properly either.

Loading a file is easy:

. configfile

Why do you have -type d in one and not in the other?

function FindMoveDelete
{
        find . -iname "${1}*.ext' -exec mv {} "${2}/${3}" \; &&
        find . -type d -iname 'FILENAME*' -exec rm -rf {} \;
}

FindMoveDelete FILENAME PATH DEST

"Instead having 9,999 functions which do the same thing with slightly different values, just have your function take parameters."

  • Well it's a good point and i would do that If I only wanted to do this action on only 1 particular kind of file but having "9.999 functions" allows me to run them all at once. If i don't want to use one I simply comment it. If I have to modify the subdirectory ($VAR) I simply edit the function.

Good point for the $PATH tho hum.. *hides :slight_smile: switched it to $DIR.

"That code couldn't possibly work. '$FILENAME' in single quotes won't expand. 'FILENAME*' won't expand properly either."

  • Indeed it's under double quotes in my script, wrote it back while writing this post, editing previous post..

"Why do you have -type d in one and not in the other?"

  • To find my "$FILENAME*" directory and delete it. The name of the directory is basicly the same as the filename. It's set in each function.

You already found and moved your FILENAME directory. The only time you'd ever delete it is when mv didn't work :wall:

If you'd tell me exactly what that function takes as inputs and exactly what it's supposed to do, I can probably find a better way.

How about this -- make a big list of things you want moved/whatever, then have your program handle this list.

while IFS="," read FILENAME DIR VAR
do
        # Skip comments
        [ "${FILENAME:0:1}" = "#" ] && continue
        # skip blank lines
        [ -z "$FILENAME" ] && continue

        function "$FILENAME" "$DIR" "$VAR"
done < ~/.thingstomove
# ~/.thingstomove
# one thing per line
# FILENAME,DIR,VAR
a,b,c
d,e,f
g,h,i
j,k,l
# skip this one
# m,n,o
p,q,r
1 Like

Well as mentionned above it's like that :

You misread.

At first I only move file with a specific extension (.ext) and if it succeeds, deletes the directory it was contained it.

For now the code is like this

 
#!/bin/sh

FindMoveDelete()
{
find . -iname "$FILENAME*.ext" -exec mv {} "$DIR/$SUBDIR" \; &&
find . -maxdepth 1 -type d -iname "FILENAME*" -exec rm -rf {} \;
}

Function1()
{
FILENAME=name.of.file.and.dir
DIR="/volume1/my/dir"
SUBDIR=Subname
FindMoveDelete
}

Function1
Function2
Function3

What it does :
Move the files "name.of.file.and.dir*.ext" to "/volume1/my/dir/Subname" and then delete the directory "name.of.file.and.dir*"

I call every functions manualy and each of them contains the function FindMoveDelete.
It kinda looks ugly but it works. If you have a better idea I m all hears, at first I only wanted to run a file containing each of those functions for a script to read and end at the end of the file (loop?)

Anyways don't go knock your head on a wall for this, it was just a simple question at first. it's working as it is :slight_smile:

Edit : Just took an other look at your last bit of code and it's interesting. Playing with IFS.. I ll test it out!
Thanks mate

It'll run rm no matter what, then :stuck_out_tongue:

I take it you're trying to search inside ${DIR}/${SUBDIR} ? Are there any sub-sub-dirs or is it flat inside?

Nop I run the script from my "in" directory only but yes for safety I ll modify the "find ." to "find /data/in" to avoid any problems.. If i happen to run it from other directories.. :slight_smile:

And no, I m not doing any "find" under $DIR/$SUBDIR I m just moving files there. Directories are already created.