![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| 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 |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
||||
|
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. |
|
||||
|
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?
|
|
||||
|
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.
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|