You are not logged in.
Pages: 1
I am writing a simple bash script to read a file line by line and add all the last words in the file.
tt=0
pline(){
shift $(($# - 1))
tt=$(($tt+$1))
}
cat cc.list | while read line; do
pline $line
done
echo $ttThe cc.list file is
pen 10
pencil 50
rubber 5I want the echo to print 65 as the result. But it is always printing 0.
Please help me.
Last edited by bharani (2009-08-22 10:09:35)
Tamil is my mother tongue.
Offline
I was beating myself over the head trying to figure this out...
http://tldp.org/LDP/abs/html/subshells.html#SUBSHELL
http://www.nucleardonkey.net/blog/2007/ … _bash.html
tl;dr: You're doing it fine, but the pipe makes a subshell in which the tt that is used is destroyed by the time the loop is finished.
Edit:
I should've thought to suggest ways around this. Try
#!/bin/bash
tt=0
pline(){
shift $(( $# - 1 ))
tt=$(($tt+$1))
}
while read line; do
pline $line
done < cc.list
echo $ttLast edited by majiq (2009-08-22 09:13:44)
Offline
Thank you.
I used this script after reading your post( before you edited it)
tt=0
pline(){
shift $(($# - 1))
tt=$(($tt+$1))
}
exec<$1
while read line
do
pline $line
done
echo "Total is " $ttLast edited by bharani (2009-08-22 10:10:34)
Tamil is my mother tongue.
Offline
Pages: 1