You are not logged in.
So I'm writing a shell script (script1). One of the things I need to do in script1 is already accomplished in a script I previously wrote (script2), however it doesn't have access to any of the functions inside script1. Is there a way to either: 1. make script2 a function of script1, or 2. export a function of script1 to the wider environment?
I realize this is very confusing, so if it needs clarifying just say so. I didn't want to bog the post down with unnecessary details about what exactly each script does and such.
Last edited by fflarex (2008-09-27 07:34:48)
Offline
Posting the scripts or some kind of similar smaller example is always nice.
Have you looked into 'export'?
Offline
Haven't read your scripts, sorry. But a quick thought: why not do this:
/usr/local/lib/my_lib:
function my_function() {
...
}
/usr/local/bin/my_script1:
source /usr/local/lib/my_lib
...use my_function...
/usr/local/bin/my_script2:
source /usr/local/lib/my_lib
...use my_function...
Offline
Make 1 file a 'library' file with functions and whatever, source that file from other files that perform logic.
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
Okay, actually I just figured out a very tricky way to accomplish this.
I can redefine a script as a function of another script like so:
function mpd-on-change {
bash <(cat ~/bin/mpd-on-change) $*
}
EDIT: Dang, this didn't actually work. Something is wrong with passing on the arguments.
Last edited by fflarex (2008-09-26 20:59:31)
Offline
you probably mean
function mpd-on-change {
bash ~/bin/mpd-on-change $*
}
but you're making it too complicated imo.. what's wrong with profjim's and mine advice?
< Daenyth> and he works prolifically
4 8 15 16 23 42
Offline
The problem is that I would have to significantly change multiple scripts if I used your guys' advice. Anyways I might not have a choice. The correction you gave me is pretty much identical to the one I had and has the same problems.
EDIT: Found a way to avoid the problem entirely.
Last edited by fflarex (2008-09-27 07:35:21)
Offline