setting a global variable in script


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers setting a global variable in script
# 1  
Old 10-23-2007
setting a global variable in script

Hi All,

I know to set global variable i can use export .. But take the situation like below ..

I want to set a variable in one script and access that in second script

i have done like this .. It is not working



one.sh
#!/usr/bin/ksh
echo $RISSHI
export RISSHI=1


two.sh
#!/usr/bin/ksh
echo $RISSHI
export RISSHI=2

the variable RISSHI is not changed to two when i run two.sh after one.sh
i tries to execute like . one.sh i got error...

Is there is way to share a variable between scripts?

Thanks,
Arun
# 2  
Old 10-23-2007
Quote:
Originally Posted by arunkumar_mca
Is there is way to share a variable between scripts?
Variables, or rather environment variables, belong to processes, not scripts, scripts change them but they exist with the process.

If you execute a script with "#!/bin/sh" it starts a new process to run the script in, hence a new set of environment variables, children will inherit the parent's exported variables but not the other way round.

The alternative is to run a script using "source" or ".". The down side of this is the script language has to be the same and the error handling must be consistent.
# 3  
Old 10-23-2007
Hi Porter ..

Thanks for your reply ..
Buy i tried to run the script like
. one.sh

and tried to acess that value but the error i get was

ksh: one.sh: not found.

please let me know whatis the change i have to do
# 4  
Old 10-23-2007
Try using a qualified path to "one.sh", either "./one.sh" or with the full directory prefixing it.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Setting a TZ variable in a script

Hello all, I know this must be simple .... but i can't grasp what could be the issue. I'm trying to setup the timezone variable (to the unix command date) according to what i find in a value that i got from parsing the config file. The end result would be setting the log file with this new... (4 Replies)
Discussion started by: maverick72
4 Replies

2. UNIX for Advanced & Expert Users

Setting a permanent global variable in unix accessible from any script

Is there anyway in which i can set a permanent global variable in unix, which when initialised with a value and modified during any shell script, would retain its value even if i logout and login I dont know whether i am being able to express my need clearly but basically what i want is a... (3 Replies)
Discussion started by: arindamlive
3 Replies

3. Shell Programming and Scripting

setting a shell script variable in awk

The following is part of a larger shell script grep -v "Col1" my_test.log | grep -v "-" | awk '$5 == "Y" {print $1}' instead of printing, can I set set $1 to a variable that the rest of the shell script can read? if $5 == Y, I want to call another shell script and pass $1 as a... (2 Replies)
Discussion started by: guessingo
2 Replies

4. Shell Programming and Scripting

Setting a variable in a while loop (.ksh script)

Hello Everyone, I'm still trying to grasp many concepts in .ksh scripting, one of them being variables inside loops. My problem is the following: * I'm trying to set a variable inside a while read loop to reuse it outside of said loop. My lines are the following :... (13 Replies)
Discussion started by: jimmy75_13
13 Replies

5. Shell Programming and Scripting

Setting environment variable using shell script

Hi All, I'm trying to write an menu driven program to automate some functions which involve loging to multiple hosts. The hosts can differ for every use, so I thought I would use an config file to get the hostnames. Now I need to set those values in the config file to environment variable to... (6 Replies)
Discussion started by: arun_maffy
6 Replies

6. UNIX for Advanced & Expert Users

Setting global variables with BASH/Linux

I am using functions in a script and for some strange reason the EXPORT command doesnt seem to be making my variables global. Anyone got any ideas? I am using one function to pass some output top another using the pipe command, eg Function 1 | Function 2 Function 2 reads the value... (3 Replies)
Discussion started by: gregf
3 Replies

7. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

8. Shell Programming and Scripting

Global Variable in a script?

How to create a Global variable within a script file. say i want a varaible called LOGFILE to be used within all the script. how to do that? (2 Replies)
Discussion started by: skyineyes
2 Replies

9. UNIX for Dummies Questions & Answers

setting global variable for all users

hi, i am a newbie unix administrator. i want to set a variable, let's say : alias cls 'clear' But i am not going to add this line in the .login file for every home directory of my 500+ users. pls tell me where should i put this line in, so that all users can use this variable after... (4 Replies)
Discussion started by: champion
4 Replies

10. UNIX for Dummies Questions & Answers

Global PATH setting

I am using Solaris 8 and I want to change the PATH setting for all users. I have edited /etc/profile, but when I log in and check the PATH variable, it hasn't changed. Am I missing something? (5 Replies)
Discussion started by: jxh
5 Replies
Login or Register to Ask a Question