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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Is it possible make the shell read functions 1 by 1 and calling an other function?
# 1  
Old 12-05-2011
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 :

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

Code:
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 :
Code:
Function1()
{
FILENAME=function1
PATH="/volume1/my/path/"
VAR=myvar
MoveAndDelete
}

But it looks ugly Smilie


Thanks for taking the time to read

Last edited by Sekullos; 12-05-2011 at 08:04 PM..
# 2  
Old 12-05-2011
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:
Code:
. configfile

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

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

FindMoveDelete FILENAME PATH DEST

# 3  
Old 12-05-2011
"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 Smilie 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.

Last edited by Sekullos; 12-05-2011 at 07:32 PM..
# 4  
Old 12-05-2011
You already found and moved your FILENAME directory. The only time you'd ever delete it is when mv didn't work Smilie

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.

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

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

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 12-05-2011
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
Code:
 
#!/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 Smilie

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

Last edited by Sekullos; 12-05-2011 at 08:04 PM..
# 6  
Old 12-05-2011
It'll run rm no matter what, then Smilie

I take it you're trying to search inside ${DIR}/${SUBDIR} ? Are there any sub-sub-dirs or is it flat inside?
# 7  
Old 12-05-2011
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.. Smilie

And no, I m not doing any "find" under $DIR/$SUBDIR I m just moving files there. Directories are already created.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. AIX

Calling functions from main program from dlopened library function

Hello All, I am trying to call a function from the calling main program from a dlopened library function, below is the entire code, when I execute it it crashes with sigill. Can you guys help me out I guess I am missing out on the linker flag or something here. besides I am new to AIX and... (1 Reply)
Discussion started by: syedtoah
1 Replies

2. Shell Programming and Scripting

Calling Pl/sql function in shell script to modify csv

I need to 1.Open a csv 2.Process the csv i.e. Modify 2 column in the csv. To modify the column the value needs to be passed to a pl/sql function and the return value should be updated For eg: If column 2 E,then E will be passed in database function which will return Employee. 3. Write a... (5 Replies)
Discussion started by: Chinky23
5 Replies

3. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

4. Shell Programming and Scripting

Calling external function in a shell

hi guys, how r u??? please I need you, help me please. I have a shell, in this shell i have this function and another code lines, this function is getting date one day back. the function is in the same shell (FILE 1) Now I need put this function in another file (FILE 2) and calling... (4 Replies)
Discussion started by: acevallo
4 Replies

5. Shell Programming and Scripting

Calling Functions of Other K Shell Program

Hi, I have a K shell a.ksh function abc { // Some logic } In b.ksh i have included the a.ksh ./a.ksh I want to call the abc function from this b.ksh script. Thanks Vijay (2 Replies)
Discussion started by: vijaykrc
2 Replies

6. Shell Programming and Scripting

calling a function in Shell script troubleshooting

Some Code After Some code part is executed the control doesnt go to rvin_doxx_scrt.. and the script exits rvin_doxx_scrt() { Some Code } if (som code) ... (4 Replies)
Discussion started by: ultimatix
4 Replies

7. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

8. Shell Programming and Scripting

Calling a C-function froma shell script

Hi, I have searched the forum for the query, But i didnt find an exact answer. I have a script(1.sh) and a c program(sample.c) sample.c contains many function definitions.( run(), find(), add() etc). I want to call functions in sample.c from 1.sh and use the return value in 1.sh... (3 Replies)
Discussion started by: jisha
3 Replies

9. UNIX for Dummies Questions & Answers

calling one function from another shell script

i have a function defined in one ksh (ksh 1) i want to use that function in another ksh (ksh 2) i am using . $<directoryname>/<ksh name> i am calling the function defined in ksh 1 in ksh 2 i want the returnstatus from the above operation but it is not executing the function what i... (1 Reply)
Discussion started by: trichyselva
1 Replies

10. UNIX for Dummies Questions & Answers

calling a c function from shell

Is it possible to call a C function from within a shell script. This C function is part of an API. What do I need to make it work from my shell script. Anybody please help. (4 Replies)
Discussion started by: seshagiri
4 Replies
Login or Register to Ask a Question