Bash scripting help needed- Functions, source, and variables

Need help with C, C++, perl, python, etc?

Bash scripting help needed- Functions, source, and variables

Postby onemyndseye » 2007-04-24 07:25

Hey guys.... should be a simple question...


I am working on a massive script (2000 lines) that is made up of mostly functions..


At this point the script has gotten so big that I need to break it up some.... So I would like to source in functions and groups of functions to make things a little easier to edit but my problem is that all variables must be accessible to all parts of the script.

For clarity:

Functions A, B and C contained in sourced filename FunctionsABC will set variables (adc="123" def="456" ghi="789") that functions D, E, and F contained in sourced filename FunctionsDEF will require.... on so forth

This is the sort of structure that I had in mind:
Code: Select all
#!/bin/bash


#  Source in functions
. /path/functionABC
. /path/functionDEF
. /path/functionGHI
. /path/functionJKL
. /path/functionMNO

# Main body of script

<blah><blah><blah>
<blah>
<blah>
<blah><blah>
<blah>
<blah><blah><blah>
<blah>
<blah><blah><blah>

exit 0



Currently it seems that variables set by the sourced functions are only available to the main body of script....not the other functions being sourced. Reguardless of why - The needed variables are not getting across :)

How to proceed? Do I simply have to make sure I export every variable in every function? This seems pretty clunky so I'm guessing there is a better way.

Thanks,
-Justin
One Mynds Eye
onemyndseye
 
Posts: 17
Joined: 2007-02-09 11:24

Postby chrismortimore » 2007-04-24 08:41

Parameter passing is your friend. Example:

Say your script is as follows:
Code: Select all
function aboringfunction()
{
    echo $STRING
}

STRING="test"
aboringfunction


Change it to:
Code: Select all
function abetterfunction()
{
    echo $1
}
abetterfunction "test"


Although, if you're starting to get to that kind of thing, I'd recommend a different language, I've never liked bash for big projets.
Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 5x320GB WD 7200rpm Caviar RE2 (RAID5), Nvidia 6600GT 256MB
Laptop: Intel Pentium M 1.5GHz, 512MB PC2700, 60GB 5400rpm IBM TravelStar, Nvidia 5200Go 64MB
User avatar
chrismortimore
 
Posts: 862
Joined: 2007-04-24 06:34
Location: Edinburgh, UK

Postby onemyndseye » 2007-04-24 08:54

Thanks for the reply...... I agree, bash isnt very well suited to big projects like this but I'm suck with it :)


your example..while handy...I'm not sure how it helps a whole lot.

some of these fuctions may create 12+ different variables... so whatever function needed them they would have to be supplied as parameters...?

Lines are more likely to look like this:
function_name <var1> <var2> <var3> <var4> <var5> <var6> <var7>

then keeping them straight as far as which order you put them in seems like a job in itself.


something like:

function_name() {
VAR1="$1"
VAR2="$2"
VAR3="$3"
VAR4="$4"
VAR5="$5"
VAR6="$6"
VAR7="$7"
}


wouldnt export be better/easier?
onemyndseye
 
Posts: 17
Joined: 2007-02-09 11:24

Postby chrismortimore » 2007-04-24 09:04

If you export variables, you have global variables, which is bad. With global variables, although it makes life easier in terms of you don't have to pass parameters, you lose security because anything can modify their contents and you either have to trust the contents is accurate (which is stupid) or validate every time you access them (which is slow).

As for the order, thats programming ;) Think of something like C++, you define a function as
Code: Select all
void afunction(int val_a, char *val_b, int val_c);

You always have to pass the parameters in the order of val_a, val_b, val_c. If you don't, it complains.

If you are not afraid of text parsing, you can do something like pass this giant string as one parameter: "val_a=1, val_b=test, val_c=50",
and parse it to assign variables correctly. But again, that is cumbersome and probably more hassle than it is worth.

And passing a lot of values isn't that uncommon in real world programming, but there are things out there to make it a little easier, like structs and classes and stuff. But I'm not sure if bash lets you use those...

Can you reduce the number of variables? Or combine some into arrays?
Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 5x320GB WD 7200rpm Caviar RE2 (RAID5), Nvidia 6600GT 256MB
Laptop: Intel Pentium M 1.5GHz, 512MB PC2700, 60GB 5400rpm IBM TravelStar, Nvidia 5200Go 64MB
User avatar
chrismortimore
 
Posts: 862
Joined: 2007-04-24 06:34
Location: Edinburgh, UK

Postby onemyndseye » 2007-04-24 09:29

".....If you export variables, you have global variables...."

Yeah... but we basically already do..yes?... right now we are working completely inside one script... all variables contained there-in are "global" in terms on they are seen and accessed by the whole script.

I'm not big on anonymous variable names like $x $i... i tend to do things like $KERNEL $MODULES_PATH $MACHINE_IDENT .... clearly and uniquely labeled... .... so the chances of the contents of one variable over-writting another is very slim.

.......LOL.... or did I miss someting in bash class? :)


"....Can you reduce the number of variables? Or combine some into arrays?..."


Hrmm... I doubt it. Its not like I just went VAR crazy....there is just alot to parse. :)

This is a bash init script that handles hardware detection and setup, machine profiling (Machines are assigned a MD5 checksum based on the hardware they contain.... makes for a unique ident across several PC's using lightweight tools like lshw, md5, and grep) and parsing of Kernel cmdline options.

The script functions very well as is...... and its sounding more and more like it needs to stay that way :)
onemyndseye
 
Posts: 17
Joined: 2007-02-09 11:24

Postby chrismortimore » 2007-04-24 09:47

onemyndseye wrote:Yeah... but we basically already do..yes?... right now we are working completely inside one script... all variables contained there-in are "global" in terms on they are seen and accessed by the whole script.
Thats bad as well ;) Things always go wrong, but with local variables, it tends to be a little less wrong.

onemyndseye wrote:The script functions very well as is...... and its sounding more and more like it needs to stay that way :)
Well, if it ain't broke... ;)
Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 5x320GB WD 7200rpm Caviar RE2 (RAID5), Nvidia 6600GT 256MB
Laptop: Intel Pentium M 1.5GHz, 512MB PC2700, 60GB 5400rpm IBM TravelStar, Nvidia 5200Go 64MB
User avatar
chrismortimore
 
Posts: 862
Joined: 2007-04-24 06:34
Location: Edinburgh, UK

Postby onemyndseye » 2007-04-24 09:59

.... well thanks for your help chris ;)


I think I will pretty much leave well enough alone.... there is a couple of the larger functions that I can break out into external scripts and reduce the "clutter" abit.....

basically to do what I would like to do would just take too much re-writing for my taste ;). You did help me learn something though :)
onemyndseye
 
Posts: 17
Joined: 2007-02-09 11:24

Postby chrismortimore » 2007-04-24 10:03

You're welcome. If I think of something useful, I'll let you know :)
Desktop: AMD Athlon64 3800+ Venice Core, 2GB PC3200, 5x320GB WD 7200rpm Caviar RE2 (RAID5), Nvidia 6600GT 256MB
Laptop: Intel Pentium M 1.5GHz, 512MB PC2700, 60GB 5400rpm IBM TravelStar, Nvidia 5200Go 64MB
User avatar
chrismortimore
 
Posts: 862
Joined: 2007-04-24 06:34
Location: Edinburgh, UK


Return to Programming

Who is online

Users browsing this forum: No registered users and 6 guests

fashionable