Just started learning Linux Bash Shell Programming, and I just don't know if I understood it correctly. Looking at the sample program below:
#!/bin/bash
n=1
sumRSS=1000
sumSZ=2000
echo Before sumRSS=$sumRSS sumSZ=$sumSZ
ps -ly | while
read c1 c2 c3 c4 c5 c6 c7 c8 c9 c10
do
if (( n>1 ))
then
echo n=$n rss=$sumRSS sz=$sumSZ
((sumRSS = sumRSS + c8))
((sumSZ = sumSZ + c9))
fi
((n++))
done
echo Sum of RSS = $sumRSS
echo Sum of SZ = $sumSZ
The output:
Before sumRSS=1000 sumSZ=2000
n=2 rss=1000 sz=2000
n=3 rss=2368 sz=29118
n=4 rss=3792 sz=55644
n=5 rss=4780 sz=82679
Sum of RSS = 1000
Sum of SZ = 2000
I don't know why the sum still goes back to RSS=1000 and SZ=2000. I actually was expecting RSS=4780 and SZ=82679.
I know I am missing something basic. I am learning bash by writing simple scripts.
set -xvin your script (just after the#!/bin/bashline). This will enable verbose mode and will help you to see what is going on. Actually in your screen bash will inform you about the code that is going to be executed (step by step) and the values of the vars... Usefull debugging options.