Bash - set global variable for multiple scripts?[SOLVED]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

Bash - set global variable for multiple scripts?[SOLVED]

#1 Post by greengeek »

How can I set variables (in one location only) that are used by multiple scripts? I want to do this without having to enter the same variables multiple times into several scripts.

I have about 5 or 6 bash scripts that I use when I run a major remaster of my puppies and these scripts are used to handle interaction between the user and the remaster process. Within these scripts I use a variable that I call "SUBTYPE" which identifies the 'flavour' or special purpose of the new puppy. This $SUBTYPE becomes grafted into various files to identify the new puppy, and also gets grafted into the name of the new iso file.

For example, the $SUBTYPE might be a name such as:
SUBTYPE=scanpup
or
SUBTYPE=testpup

or it might be a date such as:
SUBTYPE=2015_March14

At the moment I have to manually modify every one of those scripts with the new SUBTYPE variable before I do the major remaster. Is there a way to specify the SUBTYPE in one location and tell each script where to look for that variable? Where would be the best place for such a variable to be kept? Perhaps /etc/DISTRO_SPECS??

EDIT : It seems that this is easy to achieve (thanks Keef) and it is common for bash programmes to have an accompanying "config file" which contains the definitions of variables. The way to link a script to it's corresponding config (definitions) file is to add a line that tells the script where to look. For example this is how the bash script might start:

Code: Select all

#!/bin/bash
. /opt/myprogram/config
blah
blah
blah
(Note the space between the dot and the /opt/xxx)
The inclusion of the first line above will tell the script to look into file /opt/myprogram/config for a list of it's variable definitions.

The format of that config file could be something like:

Code: Select all

X="2015_version_2"
Y="Newpup_3"
DATE="2015_April_12"
STORAGE_DIRECTORY="/root/userbase"
YAFSPLASH_TEXT_1="Welcome"
YAFSPLASH_TEXT_2="Terminating"
Note to self: - the config file does not need a suffix, and does not contain the "#!/bin/bash" line which defines the script.
Last edited by greengeek on Sat 18 Apr 2015, 19:47, edited 2 times in total.

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#2 Post by Keef »

Do you mean something like this?
http://ss64.com/bash/source.html

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#3 Post by greengeek »

Thanks Keef - I don't think that's what I am looking for. (It might be, but it doesn't seem to be. Maybe it's just going straight over my head)

I was expecting something like:

SUBTYPE=/root/global_variabless.txt/"SUBTYPE"

I know that makes no real sense - but I figured there must be some way to tell the script to look for it's variable as defined in some other file.

Maybe I need to get each script to read a line from such a text file, then grep/sed/awk the variable out of that file and assign it within the current script. Seems an untidy way of doing it but there must be some way of doing this.

So I could have a text file something like /root/remaster_variables.txt and just read these "global variables" as required?

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#4 Post by Keef »

Yes, thats what 'source' does.
You can either use the word 'source' or a dot '.' followed by the filename (with path if needed) you want the script to read.
The file just needs to be a plain text file, the contents are read and included by the script that calls it. Put any variables you want in the file. Could also be functions etc.

User avatar
Keef
Posts: 987
Joined: Thu 20 Dec 2007, 22:12
Location: Staffordshire

#5 Post by Keef »

Very simple example:

Code: Select all

#!/bin/sh

. /etc/words

echo $X
echo $Y
echo $Z
The text file 'words' just contains:

Code: Select all

X="Why"
Y="are"
Z="fish?"
I'm sure the output is fairly obvious.

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#6 Post by greengeek »

Thanks for the help Keef! I finally see that it works exactly as I need - for a while there I was missing the fact that there has to be a space after the dot but with that sorted this is working very well - I can specify my 'global variable' in a single file and 'call' it from multiple scripts. Very happy.
cheers!

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#7 Post by Moose On The Loose »

greengeek wrote:Thanks for the help Keef! I finally see that it works exactly as I need - for a while there I was missing the fact that there has to be a space after the dot but with that sorted this is working very well - I can specify my 'global variable' in a single file and 'call' it from multiple scripts. Very happy.
cheers!
If this is being used as a configuration file you may want to change, I suggest you make the names of the variables in the file have a unique few characters at the start.

For example if you make it

Code: Select all

GGK_X="yes"
GGK_Y="no"
GGK_Z="maybe"
You can modify the local copy as you normally would and then rewrite the configuration file when it is time to do that with just something that looks like:

Code: Select all

set | grep "^GGK_" >${CONFIG}
You can add a save of the previous version etc without much trouble

if you want to keep content that doesn't start with the string

Code: Select all

set | grep -v "^GGK_" >${CONFIG}
set | grep "^GGK_" >>${CONFIG}

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#8 Post by greengeek »

Moose On The Loose wrote:If this is being used as a configuration file you may want to change, I suggest you make the names of the variables in the file have a unique few characters at the start.
Hi Moose - at this stage I don't fully understand your example, but it does raise some questions for me -
1) Can I set "global" variables (ie: in the config file) and then add extra variables in the script? (ie: do all relevant variables have to be declared in the "source" file?)
2) Can I specify more than one source file?
3) Is bash smart enough to "find" a variable within the source file if it is only one of, say, fifty variables? (ie: 49 irrelevant variables and one that is necessary).
4) Can I use the "source" file to override variables in the "local" script? (ie: if I define SUBTYPE="testpup" in the source file how does that affect things if I also declare SUBTYPE="testpup2" in the "local" script?
5) Are you suggesting that I can use two versions of a given variable in the same script? ie: define a variable in both "global" source file and "local" scripts, then switch the global ones off and on as desired?
(hope that makes sense...)
cheers!
.

User avatar
Karl Godt
Posts: 4199
Joined: Sun 20 Jun 2010, 13:52
Location: Kiel,Germany

#9 Post by Karl Godt »

Puppy scripts almost all of them use "source" to get "global" variables .
Example : /etc/rc.d/rc.sysinit and /etc/rc.d/rc.shutdown AND eventmanager, bootmanager, quicksetup, shutdownconfig, etc,etc,pp ...

They almost all source /etc/rc.d/PUPSTATE and /etc/DISTRO_SPECS .
Also /etc/rc.d/functions4puppy[4] are getting sourced .

Scripts in /etc/init.d/ often source /lib/lsb/functions for functions(){ :; } .

Another way to write "source" is "." .EndOfSentence

/bin/dash, that comes with the devx , does not know "source" , but "." .

There is a difference using "." :

./script would execute
. /script would source and execute commands too , but retain variables into the caller script .

"source" has a major security flaw : if source exits non-zero it usually crashes the whole script / shell if not-interactive ( controlling tty ).

So try

Code: Select all

echo "VAR1=Puppy
VAR2=Linux
VAR= GNU
"> /tmp/test.src
. /tmp/test.src
«Give me GUI or Death» -- I give you [[Xx]term[inal]] [[Cc]on[s][ole]] .
Macpup user since 2010 on full installations.
People who want problems with Puppy boot frugal :P

User avatar
greengeek
Posts: 5789
Joined: Tue 20 Jul 2010, 09:34
Location: Republic of Novo Zelande

#10 Post by greengeek »

Thanks Karl, thats good to know. I will stick with the dot.

Post Reply