LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Local vs Global variables (https://www.linuxquestions.org/questions/programming-9/local-vs-global-variables-300535/)

wujee 03-11-2005 05:08 PM

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

dustu76 03-11-2005 11:43 PM

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.


All times are GMT -5. The time now is 02:41 AM.