Using same variable in 2 different scripts that run at the same time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using same variable in 2 different scripts that run at the same time
# 1  
Old 12-15-2011
Data Using same variable in 2 different scripts that run at the same time

Hi,

I have two scripts that use the same variable. Say suppose I export that variable in script 1 and try to use it in script 2 where I have declared it locally, what will be the effective value of the variable? Will it take the exported value or the locally declared?

example:
Script1
ABC=$FILENAME
export ABC

Script2
ABC=XYZ.txt
echo $ABC --> what will be the output here
# 2  
Old 12-15-2011
Given that you overwrite ABC before you print it in script 2, ABC will always end up "xyz.txt" in that script no matter what it was before. Their values aren't protected in any way. But that's beside the point...

Variables don't travel "sideways" like that. They don't actually travel at all -- each and every program has its own, independent set of exported variables, called the 'environment'.

When you run a script or program, it gets a copy of your current environment. But the copy is unconnected. Change the child and the parent remains the same, and vice versa.

If you wanted the same variable to be set in two different programs, either set it earlier in your ~/.profile or ~/.bashrc file so it gets exported on login for you, or have a little config file thing for your scripts to source:

Code:
# littleconfigfile
# Some shells don't like export VAR=asdf.  To be safe, break it in half.
VAR=asdf
export VAR

Code:
#script1
. littleconfigfile
echo "$VAR"

Code:
#script2
. littleconfigfile
echo "$VAR"

The "." operator runs the given script inside the current shell. This is very different from just executing './littleconfigfile' because . allows you to set your own variables instead of setting variables in a useless copy.

Last edited by Corona688; 12-15-2011 at 02:50 PM..
# 3  
Old 12-15-2011
Got it! Thanx.

I actually have multiple sctipts that use the same variables in them and they all use export function in each of these scripts. My doubt is that if I would run all these scripts parallely using a single cron, will they confuse with the value of this common variable or it doesnt matter at all?

Thanks
# 4  
Old 12-15-2011
Quote:
Originally Posted by PraveenSikamani
My doubt is that if I would run all these scripts parallely using a single cron, will they confuse with the value of this common variable or it doesnt matter at all?

Thanks
Each process is its own private, independent universe. The value of 'VARIABLE' in some other process' memory doesn't matter to them one bit. All they start with is whatever values cron happens to have the instant it starts them -- which is, intentionally, almost a clean slate. And once they're started nothing from the outside affects them.

Last edited by Corona688; 12-15-2011 at 03:35 PM..
# 5  
Old 12-15-2011
Awesome! Thanks for your advice.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to run 2 python scripts at the same time side by side on the same line?

Could I run 2 python scripts at the same time side by side output on the same line in this same format but with scripts? from itertools import izip_longest with open("file1") as textfile1, open("file2") as textfile2: for x, y in izip_longest(textfile1, textfile2, fillvalue=""): x =... (4 Replies)
Discussion started by: bigvito19
4 Replies

2. Shell Programming and Scripting

How to run a sequence of scripts?

Assume I want to run a sequence of scripts: script0001.sh script0002.sh script0003.sh ... script0122.sh script0123.sh I only know a little Linux scripting, so the following is the best I can write for k in $(seq 1 123) do ./script${k}.sh done Of course it doesn't work... (2 Replies)
Discussion started by: zzzhhh
2 Replies

3. UNIX for Dummies Questions & Answers

Scripts can be run manually but couldn't run with cronjobs

I am from MQ/MB technology. My requirement is to display the queue manger and broker status on daily basis. If I manually run the script, it works fine and displays output. But when I have scheduled the same using cronjobs it shows only the queue manger status and not the broker status. Can... (3 Replies)
Discussion started by: Anusha M
3 Replies

4. Shell Programming and Scripting

Accepting multiple values in a variable at run time

Hi, Below is starting entry of my script #!/bin/ksh Usage() { print "Usage: $0 ID OPTION SERVER" print "<br>Where :" print "<br>Enter your ID into PARAM1, OPTION in the PARAM2 and SERVER in the PARAM3 field" print "<br>ID should be a valid ID" print "<br>OPTION should be either... (2 Replies)
Discussion started by: gopajitmalakar
2 Replies

5. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

6. Shell Programming and Scripting

Picking contents value of file at run time based on variable values

HI, I have to write a unix script and need your help. in my application where I have to invoke this script a varialble is there where the value comes in a variable . for example variable can be var=ABC like this there will be any type of value in the vcariable. there is a unix directory... (2 Replies)
Discussion started by: manish8484
2 Replies

7. Shell Programming and Scripting

how to run scripts....

I am going to run the scripts (filetest.sh) Its run with..... ./filetest.sh sh filetest.sh but not run with..... filetest.sh ( giviing error command not found) tell me the way how we can do it ? (6 Replies)
Discussion started by: ani83_pune
6 Replies

8. AIX

To run scripts

How to run a script from any directory? Should i include any environment or wat should i start with in writing a script ? (1 Reply)
Discussion started by: rollthecoin
1 Replies

9. UNIX for Advanced & Expert Users

how to set the environment variable at run time

hi, I have one environment variable like path in my system.But in my program i need to change that path .suppose it has a value "config" now i need to chage it as "config1" or something else.i need to use that variable for complete project.It means at first it will use the old path but after... (4 Replies)
Discussion started by: sada@123
4 Replies

10. UNIX for Dummies Questions & Answers

How to run three scripts one after another automatically???

Hi !! How do u run three scripts one after another automatically using crontab command at some specified regular interval. Say i have three scripts A,B,C and i want to run the three scripts A followed by B followed by C. REQUIRE HELP URGENTLY Thanks in advance Arunava . (3 Replies)
Discussion started by: arunava_maity
3 Replies
Login or Register to Ask a Question