Sponsored Content
Top Forums Shell Programming and Scripting global variable not being set Post 302203166 by robotball on Friday 6th of June 2008 05:57:04 PM
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.
 

9 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

9. 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
variable(n)						       Tcl Built-In Commands						       variable(n)

__________________________________________________________________________________________________________________________________________________

NAME
variable - create and initialize a namespace variable SYNOPSIS
variable ?name value...? name ?value? _________________________________________________________________ DESCRIPTION
This command is normally used within a namespace eval command to create one or more variables within a namespace. Each variable name is initialized with value. The value for the last variable is optional. If a variable name does not exist, it is created. In this case, if value is specified, it is assigned to the newly created variable. If no value is specified, the new variable is left undefined. If the variable already exists, it is set to value if value is specified or left unchanged if no value is given. Normally, name is unqualified (does not include the names of any containing namespaces), and the variable is created in the current namespace. If name includes any namespace qualifiers, the variable is created in the specified names- pace. If the variable is not defined, it will be visible to the namespace which command, but not to the info exists command. If the variable command is executed inside a Tcl procedure, it creates local variables linked to the corresponding namespace variables (and therefore these variables are listed by info locals.) In this way the variable command resembles the global command, although the global command only links to variables in the global namespace. If any values are given, they are used to modify the values of the associated namespace variables. If a namespace variable does not exist, it is created and optionally initialized. A name argument cannot reference an element within an array. Instead, name should reference the entire array, and the initialization value should be left off. After the variable has been declared, elements within the array can be set using ordinary set or array commands. SEE ALSO
global(n), namespace(n), upvar(n) KEYWORDS
global, namespace, procedure, variable Tcl 8.0 variable(n)
All times are GMT -4. The time now is 12:00 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy