You are not logged in.

#1 2008-09-26 20:04:46

fflarex
Member
Registered: 2007-09-15
Posts: 466

[SOLVED] scope issues in sh/bash...

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

#2 2008-09-26 20:14:22

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED] scope issues in sh/bash...

Posting the scripts or some kind of similar smaller example is always nice.

Have you looked into 'export'?

Offline

#3 2008-09-26 20:41:32

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: [SOLVED] scope issues in sh/bash...

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

#4 2008-09-26 20:42:10

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [SOLVED] scope issues in sh/bash...

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

#5 2008-09-26 20:47:15

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [SOLVED] scope issues in sh/bash...

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

#6 2008-09-26 21:05:12

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,001
Website

Re: [SOLVED] scope issues in sh/bash...

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

#7 2008-09-26 21:08:28

fflarex
Member
Registered: 2007-09-15
Posts: 466

Re: [SOLVED] scope issues in sh/bash...

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

Board footer

Powered by FluxBB