How to make the variables of one script available for the other scripts?


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users How to make the variables of one script available for the other scripts?
# 1  
Old 09-28-2010
MySQL How to make the variables of one script available for the other scripts?

Hello Everyone,

I want to know how can we make the variables of one script available for the other script?
for example i have three scripts variable_availability.sh,first.sh,second.sh and a file containing variables called common
----------------------------------
Code:
cat variable_availability.sh

#!/usr/local/bin/ksh
echo "starting the 1st script"
/root/Shell_Scripts/first.sh
echo "1st script finished"
echo "starting 2nd script"
/root/Shell_Scripts/second.sh
echo "2nd script finished"

--------------------------------
Code:
cat first.sh
#!/usr/local/bin/ksh
.  /root/Shell_Scripts/common
echo "The value of a=$a before modification"
a=`expr $a - 2`
echo "The value of a=$a after modification in 1st script"
echo "The value of b=$b before modification"
b=`expr $b + 2`
echo "The value of b=$b after modification in 1st script"

---------------------------------
Code:
cat second.sh
#!/usr/local/bin/ksh
.  /root/Shell_Scripts/common
echo "The value of a=$a in second script"
echo "the value of b=$b in second script"

----------------------------------------------
Code:
cat common

#!/usr/local/bin/ksh
a=10
b=20

--------------------------------------------
The output while running the variable_availability.sh script is as follows,
--------------------------------------------
Code:
starting the 1st script
The value of a=10 before modification
The value of a=8 after modification in 1st script
The value of b=20 before modification
The value of b=22 after modification in 1st script
1st script finished
starting 2nd script
The value of a=10 in second script
the value of b=20 in second script
2nd script finished

----------------------------------------------
The modified variables 'a' and 'b' in first.sh is not available for second.sh
Please share with me if anybody has any idea on this one.
Thanks in advance..........Smilie
Moderator's Comments:
Mod Comment Please use [CODE][/CODE] tags where appropriate, thank you

Last edited by pludi; 09-28-2010 at 06:30 AM..
# 2  
Old 09-29-2010
You write desired "export VAR=VAL" into a temp file and execute "./temp" before "/root/Shell_Scripts/second.sh". In other way you can write "VAR=VAL" into temp file and execute ". temp" before "/root/Shell_Scripts/second.sh"
# 3  
Old 09-30-2010
Quote:
Originally Posted by sumitpandya
You write desired "export VAR=VAL" into a temp file and execute "./temp" before "/root/Shell_Scripts/second.sh". In other way you can write "VAR=VAL" into temp file and execute ". temp" before "/root/Shell_Scripts/second.sh"
i tried as you said it's not working, i can't make those variables as global..cause the number of variable's are more.....Smilie

but the second method worked thanks....

---------- Post updated at 11:51 PM ---------- Previous update was at 11:40 PM ----------

I got it...
at the end of first.sh script
i wrote those variables to file called var_temp.sh and i included that one in the second script like this as follows,

cat second.sh

#!/usr/local/bin/ksh
. /root/Shell_Scripts/var_temp.sh
echo "The value of a=$a in second script"
echo "the value of b=$b in second script"

the output came in this way,

starting the 1st script
The value of a=10 before modification
The value of a=8 after modification in 1st script
The value of b=20 before modification
The value of b=22 after modification in 1st script
1st script finished
starting 2nd script
The value of a=8 in second script
the value of b=22 in second script
2nd script finished

Thanks sumitpandya...

Last edited by Kesavan; 09-30-2010 at 01:50 AM.. Reason: suggestion worked
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. UNIX for Dummies Questions & Answers

Trying to make a script to run 3 other scripts in a screen.

Hello, my name is Spurkle. I'm new to linux stuff. Currently I am trying to make a script which will run three other scripts in screen. This is the code: sudo screen -S hubServ /var/servers/hub/hub.sh sudo screen -S facServh /var/servers/factions/factions.sh sudo screen -S bunServ... (5 Replies)
Discussion started by: Spurkle
5 Replies

3. Shell Programming and Scripting

Using a script to define variables and run multiple other scripts in succession.

I'm pretty new to scripting in Korn shell so please forgive me... What I'm trying to do is to create a script that calls multiple other ksh scripts and defines variables for text files. I need it to define my user defined variables (file paths, date & time stamps, etc that are currently in... (1 Reply)
Discussion started by: bluejwxn8
1 Replies

4. UNIX for Dummies Questions & Answers

Tips to make scripts install

Hi, I have a series of BASH shell scripts that I would like to package and distribute. I would like to make it as easy as possible users to install the package on their system. The only tasks that need to be performed for installation are 1) unzip the package, 2) modify the user's ~/.bashrc... (1 Reply)
Discussion started by: msb65
1 Replies

5. UNIX for Advanced & Expert Users

Script to make a table from Named Variables.

I need a shell script to make a table from Named Variables Input File(Can have multiple lines): a=1,b=2,d=4,e=5 a=11,b=12,c=13,d=14 Output file: a,b,c,d,e 1,2,,4,5 11,12,13,14, Thanks in advance (7 Replies)
Discussion started by: shariramani
7 Replies

6. Shell Programming and Scripting

How to make variables in script function local?

Is it possible to make function variables local? I mean for example, I have a script variable 'name' and in function I have declared variable 'name' I need to have script's 'name' have the same value as it was before calling the function with the same declaration. The way to preserve a... (5 Replies)
Discussion started by: alex_5161
5 Replies

7. 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

8. Shell Programming and Scripting

How to make the same change in multiple shell scripts?

I would like to make the same change in multiple shell script files and would like to know if anyone can be of some help? I would appreciate it. (4 Replies)
Discussion started by: rdakhan
4 Replies

9. Shell Programming and Scripting

is there any way to make flat file from some of scripts ?

Hello is there any way to if lets say i have main.pl script , but i have 3 includes in this perl script now i will like to some how to treat this main.pl and its includes files as single file something like -E (Preprocess only; do not compile, assemble or link) in the c compilers . in short i... (0 Replies)
Discussion started by: umen
0 Replies

10. UNIX for Dummies Questions & Answers

Regarding Make Scripts

Hi I'm well worsed in Shell Scripting on UNIX OS. But I found the script used in Make file for compiling the unix sources is quite criptic. Even though it looks some what like any other Shell Script, but I noticed lots of difference & I'm always facing difficult in understanding the code... (5 Replies)
Discussion started by: S.Vishwanath
5 Replies
Login or Register to Ask a Question