LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-11-2005, 06:08 PM   #1
wujee
LQ Newbie
 
Registered: Jan 2005
Posts: 2

Rep: Reputation: 0
Question Local vs Global variables


Dear all,

I have following code, ran under both sun solaris and HP unix, I got the same results,

Code: test.sh
WRK_FILE=$HOME/.ckfile
UID1="test1"
UID2="test2"
nxt_uid=" "
cur_uid=" "

if [[ -s $WRK_FILE ]]; then
cat $WRK_FILE | while read line
do
flag=$line
echo begining flag: $flag
if [ $flag = $UID1 ]; then
cur_uid=$flag
nxt_uid=$UID2
echo current uid in $WRK_FILE is: $cur_uid, and the next uid is: $nxt_uid
echo $nxt_uid > $WRK_FILE
elif [ $flag = $UID2 ]; then
cur_uid=$flag
nxt_uid=$UID1
echo current uid in $WRK_FILE is: $cur_uid, and the next uid is: $nxt_uid
echo $nxt_uid > $WRK_FILE
else
echo "The $WRK_FILE file is damaged and please check the load shell program"
fi
done
uidcurrent=$cur_uid
echo uidcurrent: $uidcurrent
else
echo "The $WRK_FILE file does not exist"
fi

echo end flag: $flag
echo cur_uid: $cur_uid
echo nxt_uid: $nxt_uid


Run results:
$ ./test.sh
begining flag: test1
current uid in /home/.ckfile is: test1, and the next uid is: test2
uidcurrent: test1
end flag: test1
cur_uid: test1
nxt_uid: test2


However, the same code executed in Red Hat Linux 9.0, in either ksh, csh, bash, tcsh, or bsh, the code didníŽt work, and I got the following results:

begining flag: test1
current uid in /home/.ckfile is: test1, and the next uid is: test2
uidcurrent:
end flag:
cur_uid:
nxt_uid:


To me, it looks like the local and global variables issue. I am not sure if Linux works different than HP or Sun Solaris? If it is, how differences they are and what they are? What changes do I need to make to make this code work? Any advice will be most appreciated!


Thanks,
Wujee
 
Old 03-12-2005, 12:43 AM   #2
dustu76
Member
 
Registered: Sep 2004
Distribution: OpenSuSe
Posts: 153

Rep: Reputation: 30
We recently had another thread on a similar issue...

Instead of this:
Code:
cat $WRK_FILE | while read line
do 
...
done
Try this:
Code:
while read line
do 
...
done < $WRK_FILE
HTH.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
g++ complains about global variables copolii Programming 3 09-18-2005 09:43 PM
where does static and global variables stored raviksp General 0 04-28-2005 01:23 AM
global variables in php champ Programming 2 10-13-2002 02:31 PM
exporting global variables neo77777 Programming 6 09-18-2002 02:46 PM
global environmental variables antadam Linux - General 2 10-11-2001 07:36 AM


All times are GMT -5. The time now is 05:56 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration