Setting global variables with BASH/Linux


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Setting global variables with BASH/Linux
# 1  
Old 11-20-2008
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 passed from Function one and sets some variables based on its value then exports them

setStartSeq() {
read logFile
echo "Start redo log: $logFile"
arcPrefix=$(echo $logFile | cut -d_ -f1)"_"$(echo $logFile | cut -d_ -f2)
startSeq=$(echo $logFile | cut -d_ -f3 | cut -d. -f1)
export arcPrefix=$arcPrefix
export startSeq=$startSeq
}


Yet later on in the script another function tries to use the above exported variables and they arent set!!

I have confirmed that by running an export -p in the later function and all values set above are blank.

I am new to BASH and Linux but have used export without issue with kornshell.


Any help would be greatly appreciated Smilie
# 2  
Old 11-20-2008
i assume the functions are running in a subshell depending on how you ionvoke them.
The subshell is closed after execution an the variables are lost. export does only work from parent to child.
# 3  
Old 11-20-2008
Yes it does, but I dont know how else to do it.

I have a function that extracts data from a database, which another function reads. This stores a starting sequence number.

Then the script performs various actions.

Then at the end another function needs to get the starting sequence number to process some files.

Here are the functions and how they are called. The first two pipelined are the ones extracting/reading the data from within the database, then the bottom one needs to get the value read by setStopSeq

getLogFile | setStopSeq
copyArc
# 4  
Old 11-20-2008
You shouldn't have to use export to make variables within a function become global. Once a function is run, it's variables should be available throughout the rest of your script.

If you are invoking a subshell (while statement?) in one of your functions, that's another story. It would help to see the other functions to debug this problem.

Are you certain that the values from Function 1 are being passed to Function 2 when using the pipe? If you want to pass a variable to your second function, I'd do it like this:

Code:
func1 () {
var1=1
}

func2 () {
var2=$1
}

func1
func2 $var1

echo $var2

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash: Setting default values for variables

I have a variable I want to use in bash script. The user will pass an argument to the script and I will store it in `arg_fql`. If the user does not pass the variable, I still never set arg_fql, but I set another variable to a default. However, if the user passes a value, `arg_fql` will be set to... (2 Replies)
Discussion started by: kristinu
2 Replies

2. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

3. UNIX for Dummies Questions & Answers

Global variables in perl

hi all, i need a help for the following query. Thanks in advance for your valuable time. i have a main.pl file which has a global variable declared as below. our myVar=0; call first.pl script from the main.pl script. print the value of myVar (the value is still 0 and not 10.) i have a... (1 Reply)
Discussion started by: hemalathak10
1 Replies

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

5. Solaris

How to access ENV variables of non global zones in global zone???

Hi Guys, My requirement is I have file called /opt/orahome/.profile in non global zone. PATH=/usr/bin:/usr/ucb:/etc:/usr/sbin:/usr/local/bin:/usr/openwin/bin:. export PATH PS1="\${ORACLE_SID}:`hostname`:\$PWD$ " export PS1 EDITOR=vi export EDITOR ENV=/opt/orahome/.kshrc export ENV... (1 Reply)
Discussion started by: vijaysachin
1 Replies

6. UNIX for Dummies Questions & Answers

global variables

Hi, I hav created a script that calls a sub-script. In both the scripts i called the configuration file. Now i wanted to use a variable that should be used in both script and sub-script files. Actually, i wanted to return a file name and the return status to the script file from the sub-script.... (6 Replies)
Discussion started by: Swapna173
6 Replies

7. UNIX for Dummies Questions & Answers

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... (3 Replies)
Discussion started by: arunkumar_mca
3 Replies

8. Shell Programming and Scripting

perl global variables

Can someone give me "the lecture" on why you shouldn't make all your varables global when programming in perl. I have been doing this but I have heard that it is not a good practice. (3 Replies)
Discussion started by: reggiej
3 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