How to declare variables once and reuse them in other scripts?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to declare variables once and reuse them in other scripts?
# 1  
Old 02-06-2011
How to declare variables once and reuse them in other scripts?

Hello everyone.

I'm trying to create a conf file with variables that my other scripts will use.

I have several scripts that use the same variables, and since I don't know how to read them from an external file, i define them in each script (and then if i want to change one's value i need to change it separately in each script.)

so i'm trying to create a conf file that will look like that:
Code:
var1="aaa"
var2="bbb"
etc...

and then within each script refer to this file:
Code:
var1= (var 1 from external conf file)
var2 = (var 2 from external conf file)

I'd appreciate any guidance in this regard,

Thanks
Moshe

Moderator's Comments:
Mod Comment Please use code tags, and a more descriptive subject title next time

Last edited by Scott; 02-06-2011 at 11:53 AM.. Reason: Code tags; Changed Subject title
# 2  
Old 02-06-2011
Hi.

Put all your declarations in a separate file, and then source that file in your other scripts.

i.e.
Code:
$ cat /Users/scott/myVars.sh
var1=123
var2=abc

$ cat myScript
. /Users/scott/myVars.sh
echo var1 is $var1
echo var2 is $var2

$ ./myScript
var1 is 123
var2 is abc

# 3  
Old 02-06-2011
Thanks Scott.
I just tried it and it seems to work.
Which is way simpler then i thought (:

so by typing . /(filename)
i add the content of the file to the script?
or what does . / exactly do?
# 4  
Old 02-06-2011
Forget the /, that's part of the filename.

The important thing is the dot (.), or you can use the word source, if you prefer.

From the bash man-page:
Code:
        .  filename [arguments]
       source filename [arguments]
              Read and execute commands from filename in the current shell environment and return the exit  status  of
              the  last  command executed from filename.  If filename does not contain a slash, file names in PATH are
              used to find the directory containing filename.  The file searched for in PATH need not  be  executable.
              When  bash  is not in posix mode, the current directory is searched if no file is found in PATH.  If the
              sourcepath option to the shopt builtin command is turned off, the PATH is not searched.   If  any  argu-
              ments  are  supplied,  they  become  the positional parameters when filename is executed.  Otherwise the
              positional parameters are unchanged.  The return status is the status of the last command exited  within
              the script (0 if no commands are executed), and false if filename is not found or cannot be read.

This User Gave Thanks to Scott For This Post:
# 5  
Old 02-06-2011
Awsome! thanks!!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Need to get out of 2 variables in scripts

Hi All, i have written below script, and out put i am looking for both variable PRIMARY_CONF and $STANDBY_CONF but i am getting below error d1.sh: line 64: ------------------------------ line 64 is: if -a ; then ---------------------------------- please let me know where is the... (9 Replies)
Discussion started by: amar1208
9 Replies

2. Shell Programming and Scripting

How to pass variables between scripts?

Hello, I have two bash scripts like the following: script 1: #!/bin/bash var=WORLD bash path/to/second/script/script2.bash script 2: #!/bin/bash echo "HELLO $var" I expected the output to be "HELLO WORLD" but instead, I get "HELLO". I understand that when I envoke another bash... (2 Replies)
Discussion started by: jl487
2 Replies

3. Shell Programming and Scripting

Passing variables between sub-scripts

I have written a program for some data analysis. It is gettin long. I would like to restructure it so that i can have a master file which controls multiple subscripts in order to make it easier to understand. I need to be able to define variables in the master script which are used by all three... (2 Replies)
Discussion started by: carlr
2 Replies

4. Shell Programming and Scripting

Scope of variables between scripts

Friends, I am using ksh under SunoS. This is what I have In file1.sh NOW=$(date +"%b-%d-%y") LOGFILE="./log-$NOW.log" I will be using this file through file1.sh as log file. I have another script file2.sh which is being called inside my file1.sh. I would like to use the same log... (6 Replies)
Discussion started by: dahlia84
6 Replies

5. Shell Programming and Scripting

Passing variables between scripts

Hi all. I need to pass a value from a script that runs in a sub-shell, back into the calling shell like below (or into script 2 directly): outer_script export param=value1 script1 $param (in script1: export param=value2) script2 $param ($param is now value1, not value2... (4 Replies)
Discussion started by: bbergstrom74
4 Replies

6. UNIX for Dummies Questions & Answers

How to declare global variables for shell script

Hi, I do have some variables accessed in more than one script. I want to have those variables in a command file anduse them. Something like a header file that we use in C programs. I dont want to set them as environment variables. Is there any other option, like header file in scripting ?? ... (2 Replies)
Discussion started by: risshanth
2 Replies

7. UNIX for Dummies Questions & Answers

Passing variables between scripts...

Hey all, I'm wondering how you pass variable's that are defined in one script to another script that's been called by that first script..... Best regards, Jaz (1 Reply)
Discussion started by: Jazmania
1 Replies

8. Shell Programming and Scripting

How to access variables across scripts

Hi All, I have declared a variable in script1 and assign a value for it. In script2 i'll call script1 and then I want the value of variables set in script1. I have tried with export, but in vain. How can I achive this? Below is the two scripts. --script1 #!/usr/bin/ksh echo $1... (1 Reply)
Discussion started by: javaDev
1 Replies

9. UNIX for Dummies Questions & Answers

Variables in scripts

Just a quick question. If I have a script that calls another script while running, is it possible for the second script to reference a variable in the first script and if so, how. Is it scriptname.sh:$VARIABLE for a global variable and can you do scriptname.sh:function $VARIABLE or am I off my... (1 Reply)
Discussion started by: kirkm76
1 Replies

10. Shell Programming and Scripting

declare, assign variables using array, counter, loop

using bash Tru64... converting DCL to shell... any tips to make this work would be greatly appreciated. Below is my failed attempt to assign command line input to variables by first declaring an array. I use a counter to create unique variables in a loop through the array. I need to call... (3 Replies)
Discussion started by: egkumpe
3 Replies
Login or Register to Ask a Question