Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Thu 03 Sep 2015, 19:29
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Bash - set global variable for multiple scripts?[SOLVED]
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [10 Posts]  
Author Message
greengeek

Joined: 20 Jul 2010
Posts: 3221
Location: New Zealand

PostPosted: Fri 17 Apr 2015, 15:18    Post subject:  Bash - set global variable for multiple scripts?[SOLVED]  

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:
#!/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:
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, 15:47; edited 2 times in total
Back to top
View user's profile Send private message 
Keef


Joined: 20 Dec 2007
Posts: 708
Location: Staffordshire

PostPosted: Fri 17 Apr 2015, 17:17    Post subject:  

Do you mean something like this?
http://ss64.com/bash/source.html
Back to top
View user's profile Send private message 
greengeek

Joined: 20 Jul 2010
Posts: 3221
Location: New Zealand

PostPosted: Fri 17 Apr 2015, 17:33    Post subject:  

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?
Back to top
View user's profile Send private message 
Keef


Joined: 20 Dec 2007
Posts: 708
Location: Staffordshire

PostPosted: Fri 17 Apr 2015, 17:42    Post subject:  

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.
Back to top
View user's profile Send private message 
Keef


Joined: 20 Dec 2007
Posts: 708
Location: Staffordshire

PostPosted: Fri 17 Apr 2015, 18:09    Post subject:  

Very simple example:

Code:
#!/bin/sh

. /etc/words

echo $X
echo $Y
echo $Z


The text file 'words' just contains:
Code:
X="Why"
Y="are"
Z="fish?"


I'm sure the output is fairly obvious.
Back to top
View user's profile Send private message 
greengeek

Joined: 20 Jul 2010
Posts: 3221
Location: New Zealand

PostPosted: Sat 18 Apr 2015, 05:45    Post subject:  

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!
Back to top
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 609

PostPosted: Sun 19 Apr 2015, 10:36    Post subject:  

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:

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:

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:

set | grep -v "^GGK_" >${CONFIG}
set | grep "^GGK_" >>${CONFIG}
Back to top
View user's profile Send private message 
greengeek

Joined: 20 Jul 2010
Posts: 3221
Location: New Zealand

PostPosted: Mon 20 Apr 2015, 02:24    Post subject:  

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!
.
Back to top
View user's profile Send private message 
Karl Godt


Joined: 20 Jun 2010
Posts: 4027
Location: Kiel,Germany

PostPosted: Mon 20 Apr 2015, 06:13    Post subject:  

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:
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 Razz
Back to top
View user's profile Send private message 
greengeek

Joined: 20 Jul 2010
Posts: 3221
Location: New Zealand

PostPosted: Mon 20 Apr 2015, 15:23    Post subject:  

Thanks Karl, thats good to know. I will stick with the dot.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [10 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1711s ][ Queries: 11 (0.0192s) ][ GZIP on ]