The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
.
google unix.com



UNIX for Advanced & Expert Users Expert-to-Expert. Learn advanced UNIX, UNIX commands, Linux, Operating Systems, System Administration, Programming, Shell, Shell Scripts, Solaris, Linux, HP-UX, AIX, OS X, BSD.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
global variables in KLD (FreeBSD) int80h High Level Programming 0 01-17-2008 08:14 PM
setting a global variable in script arunkumar_mca UNIX for Dummies Questions & Answers 3 10-23-2007 07:46 AM
perl global variables reggiej Shell Programming and Scripting 3 08-29-2005 08:18 AM
setting global variable for all users champion UNIX for Dummies Questions & Answers 4 08-15-2002 12:59 AM
Global PATH setting jxh UNIX for Dummies Questions & Answers 5 07-20-2001 05:23 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-20-2008
gregf gregf is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
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
  #2 (permalink)  
Old 11-20-2008
demwz demwz is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 128
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 (permalink)  
Old 11-20-2008
gregf gregf is offline
Registered User
  
 

Join Date: Nov 2008
Posts: 2
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 (permalink)  
Old 11-20-2008
stanleypane stanleypane is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 23
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
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:44 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0