Global variable becomes local


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Global variable becomes local
# 1  
Old 10-29-2004
Global variable becomes local

I have encountered a very weird behavior of a global variable in Korn Shell in AIX:
A function f1 in my script pipes the output of the function f2 to a program.
A variable defined as global using typeset gets its value in f2.
That value is not seen in f1. If I remove the pipe ksh recognizes the variable as global.
Can anybody explain this?


#!/usr/bin/ksh

typeset var

function f2
{
var=1111
}

function f1
{
typeset lpt=tmp
var=alpha
f2 | wc $lpt
print "after f2 call, \$var is $var"
}

f1

Smilie
# 2  
Old 10-29-2004
some thing wrong with the line

f2 | wc $lpt in tr script
# 3  
Old 10-29-2004
Lightbulb

Sorry, should be
f2| wc >$lpt

or just

f2 | wc

Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

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

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

3. UNIX for Dummies Questions & Answers

Global - Local script??

Hi, I have a script which can be called from any path on the machine. It kind of acts like a global script. How do I achive this? :confused: The path from which I call it is different from the path where it exists. (where <script name> ) Thanks and Regards, Preetham R. (1 Reply)
Discussion started by: preethgideon
1 Replies

4. HP-UX

Seeing different output from who -u in global INIT and local SRP

I am seeing different output from "who -u" when we execute "who -u" in global INIT and local SRP after calling telnet, rlogin and remsh. We are not seeing correct output when we login to local SRP using telnet, rlogin and remsh. srp_init just creates an UTMP entry in the UTMP database. Based my... (1 Reply)
Discussion started by: madhur.tripathi
1 Replies

5. Solaris

sharing a directory between local and global zone

is this the step? add fs set dir=/export set special=/export set type=lofs add options rw end i notice i can't post immediately, moderator needs to moderate. i have 1 more post still haven't appear in the forum..hmm.... (1 Reply)
Discussion started by: binary0011
1 Replies

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

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

8. Shell Programming and Scripting

Problem with global and local variables

Guys, how can I define global variables in sorlaris...cause I lose the values outside the scope. Rite now wat I do is,I redirect variable value to a file n then get it back outside the function...:o....theres obviously a better way of doing this...I now this is a basic question....but please... (2 Replies)
Discussion started by: qzv2jm
2 Replies

9. Programming

Reg. Local vs Global declarations

Please go through the following two versions of code :- Version 1 --- string1 and string2 declared as Global variables The output is :-- (as expected sprintf is overwriting the first byte of string2 with NULL) string1=send string2= #include <stdio.h> char string1; char string2; main()... (2 Replies)
Discussion started by: kms
2 Replies
Login or Register to Ask a Question