You are not logged in.

#1 2011-01-29 20:23:39

le grand fuzzy
Member
Registered: 2007-10-31
Posts: 8

variable scope in zsh

I'm trying to write a zsh script for dzen, and I'm not sure to change a global variable from within a function. With the following function it seems to be creating a local RXB and TXB instead of updating the global ones:

fnet() {
   RXBN=`cat /sys/class/net/${NET_DEVICE}/statistics/rx_bytes`
   TXBN=`cat /sys/class/net/${NET_DEVICE}/statistics/tx_bytes`
   RXR=$(((RXBN - RXB) / 1024 / NETIVAL)) 
   TXR=$(((TXBN - TXB) / 1024 / NETIVAL)) 
   TEMP="r0 $RXB r1 $RXBN r $RXR ... t0 $TXB t1 $TXBN t $TXR"
   RXB=$((RXBN))
   TXB=$((TXBN))

   print $TEMP
   #print "${NET_DEVICE}: dn ${RXR} kB/s up ${TXR} kB/s"
}

The full script: http://sprunge.us/HJEa?sh

How do I fix this?

Offline

#2 2011-01-30 22:16:58

milomouse
Member
Registered: 2009-03-24
Posts: 940
Website

Re: variable scope in zsh

i can't run your script since i don't have those files/folders so i'm somewhat confused, trying to decipher how it should work. you have RXB as the same parameter as RXBN in the function and RXB updating as RXBN? are you getting any errors when you run the script, such as a command not found or does it just show the same parameters that don't update?

Offline

Board footer

Powered by FluxBB