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 > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
global variable in awk anhtt Shell Programming and Scripting 1 03-16-2008 03:55 PM
setting a global variable in script arunkumar_mca UNIX for Dummies Questions & Answers 3 10-23-2007 07:46 AM
Global Variable in a script? skyineyes Shell Programming and Scripting 2 07-12-2007 06:55 AM
Global Variable in awk... ZINGARO Shell Programming and Scripting 1 07-04-2007 01:07 PM
Global variable becomes local odashe318 Shell Programming and Scripting 2 10-29-2004 09:18 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 06-06-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
global variable not being set

In ksh I thought a global variable was any variable in a script or function that did not have the typeset command. I have a global in my calling script which I increment in a function, but the value does not change in the calling script. Here is the code:

function f_open_log
{
typeset -r TRUE=0
typeset -r FALSE=1
typeset -r SUCCESS=0
typeset -r FAILURE=1
typeset append=${FALSE}
typeset no_timestamp=${FALSE}

[[ $# -gt 0 ]] && [[ "x$1" = x-a ]] && { append=${TRUE}; shift; }
[[ $# -gt 0 ]] && [[ "x$1" = x-nt ]] && { no_timestamp=${TRUE}; shift; }

[[ $# < 1 ]] && return $FAILURE

next_fh=${LOG_FH_COUNTER}

if [[ ${append} == ${FALSE} ]] ; then
f_file_exists "${1}"
[[ $? == ${FAILURE} ]] && return $FAILURE

f_filechk "${1}"
[[ $? == ${FAILURE} ]] && return $FAILURE

eval "exec $next_fh>$1"
else
f_file_exists -i "${1}"
[[ $? == ${FAILURE} ]] && return $FAILURE

eval "exec $next_fh>>$1"
fi

[[ ${no_timestamp} == ${FALSE} ]] && [[ ${append} == ${FALSE} ]] && f_print_log -nv $next_fh "Log file opened"
[[ ${no_timestamp} == ${FALSE} ]] && [[ ${append} == ${TRUE} ]] && f_print_log -nv $next_fh "Log file re-opened"

(( LOG_FH_COUNTER=LOG_FH_COUNTER + 1 ))

print ${next_fh}
return $SUCCESS
}
******************************
#!/bin/ksh

. f_log.sh

### GLOBALS
VERBOSE=0
LOG_FH_COUNTER=3

echo "LOG_FH_COUNTER START = ${LOG_FH_COUNTER}"


### OPEN LOG
LOG=$(f_open_log myfile)
[[ $? == 1 ]] && print "ERROR in $0" && exit 1

echo "LOG_FH_COUNTER HERE = ${LOG_FH_COUNTER}"

LOG2=$(f_open_log bigfile)
[[ $? == 1 ]] && print "ERROR in $0" && exit 1

echo "LOG_FH_COUNTER AGAIN = ${LOG_FH_COUNTER}"

exit
*************************************************
The value of LOG_FH_COUNTER remains 3 although it should increment to 4 and 5. I did have a print statement in the function and I see the value changing to 4, but when it returns to the calling script it's 3.

Any help would be greatly appreciated. Thanks.
  #2 (permalink)  
Old 06-06-2008
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
LOG2=$(f_open_log bigfile)

In order to process the above line, ksh must launch a sub-shell. The variable is changed in a global manner in that sub-shell, but then it exited.
  #3 (permalink)  
Old 06-09-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
Is there anyway around this? I'm really stuck at this point. Thanks.
  #4 (permalink)  
Old 06-09-2008
robotball robotball is offline
Registered User
  
 

Join Date: Jun 2008
Posts: 8
I tried EXPORT of the variable from the function, but this failed to work as well. Is there a way to call the function without the creation of the subshell? I could use the value passed back as an input to the function, however that seems like a bad work-around. Any ideas?
  #5 (permalink)  
Old 06-10-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
No, there is no way for a subshell to modify its parent's environment. You need to invoke it differently. Perhaps change the function so that it prints the new value, and invoke it like VALUE=$(function $VALUE) or something like that. eval is another option.
  #6 (permalink)  
Old 06-10-2008
fpmurphy's Avatar
fpmurphy fpmurphy is offline Forum Staff  
Moderator
  
 

Join Date: Dec 2003
Location: Florida
Posts: 1,913
Here is a counter example
Code:
#!/bin/ksh93


integer COUNT=1

function inc
{
    ((COUNT++))

    print "$COUNT"
    return 0
}


print "1st: $COUNT"
COUNT=$(inc)
print "2nd: $COUNT"
COUNT=$(inc)
print "3rd: $COUNT"

exit 0
Code:
1st: 1
2nd: 2
3rd: 3
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 05:31 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