global variable not being set


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting global variable not being set
# 1  
Old 06-06-2008
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  
Old 06-06-2008
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  
Old 06-09-2008
Is there anyway around this? I'm really stuck at this point. Thanks.
# 4  
Old 06-09-2008
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  
Old 06-10-2008
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  
Old 06-10-2008
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

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unable to set Global variable outside while loop

Below is my code: count=0 if ... ... else ... find * -prune -type d | sort -r -n | while read d; do count=1 if ; then echo "Count1:$count" ... ... break 2; fi ... done ... fi echo "Count2:$count" (9 Replies)
Discussion started by: mohtashims
9 Replies

2. Shell Programming and Scripting

Question around Global Variable

Hi, I am using Linux and sh shell count=7 find * -prune -type d | sort -r -n | ( while read d; do if ; then echo "FOUND COUNTER1 is: $count" break 2; fi done echo "FOUND COUNTER2 is: $count" ) if ; then echo "Problem: Multiple or NO records...Please CHECK !!" fi Output: ... (4 Replies)
Discussion started by: mohtashims
4 Replies

3. Shell Programming and Scripting

Global Variable

Hi, I have created a variable say today at the begin having 123 as its value and inside a for loop it gets resolved to some value say 150 in its first iteration. How can I use this value 150 ( 1st iteration's ) outside the scope of for loop ?. In the same way I wanted to use all iteration's... (1 Reply)
Discussion started by: penqueen
1 Replies

4. Solaris

How to set a global alias in UNIX Solaris?

Defined a user alias in unix. Not able to use that alias in a ksh scritp. I dont want to change/source in the script to set the value. Need to set the user alias as a gloabal alias to use it in a new ksh shell window. Can you please help on this? (3 Replies)
Discussion started by: rbalaj16
3 Replies

5. UNIX for Dummies Questions & Answers

set global rm command

Hi, My team does not have root access however many a times my members logon to the unix server and fire the rm command mistakenly on wrong folder and later realise that they have made a mess. The rm -i option prompts for a confirmation before actually deleting the files/folders. I want to... (5 Replies)
Discussion started by: shifahim
5 Replies

6. Shell Programming and Scripting

Global variable value

Hi All, Im new to shell scripting. I am running EgA.sh and setting one global variable XYZ=0 . Also calling another EgB.sh from EgA.sh, changing the value of XYZ=10 but after executing EgB.sh, value of XYZ is still 0. Im expecting it to be 10. Anyone for help. Thanks in Advance. :) (5 Replies)
Discussion started by: paliwal
5 Replies

7. Shell Programming and Scripting

Help with Global Variable

Hi Guyz, I have a requirement like, i have to run a script every hour to count the number of errors encountered. At the end of the day, i need to send them the total number of errors, that have ocurred the entire day. For eg. if 10 errors occurred for starting 1 hr, 5 for next 1 hr, so on.... (1 Reply)
Discussion started by: DTechBuddy
1 Replies

8. Shell Programming and Scripting

Global variable

I have written a shell scritp in which i am using a variable which is declared before a while loop and i am updaitng the variable in while loop and want to use its updated value outside the loop. I am not able to do so, b'coz the scope of the variable is limited to the while loop only and when i am... (5 Replies)
Discussion started by: deepanshu
5 Replies

9. Shell Programming and Scripting

Global Variable in awk...

i think..... it's possible use a variable out a awk in the awk ??? ex. A=20071225 awk '{ print "the value is" $a }' OR awk '{ print "........"; c=10; print $c ; c=$A ; print $A}' for a external variable is possbile use in the awk ??? (1 Reply)
Discussion started by: ZINGARO
1 Replies
Login or Register to Ask a Question