You are not logged in.
Hi.
The real code is:
TOTAL_UPG=0
TOTAL_INS=0
TOTAL_DEL=0
STRING_UPG=`echo $1|sed -e 's/\[/\\\[/g;s/\]/\\\]/g'`
LINEA_UPG=`grep -n "$STRING_UPG" $PACMANLOG|awk -F: '{print $1}'`
tail -n `expr $NUM_LINES_PACLOG - $LINEA_UPG 2>/dev/null` $PACMANLOG 2>/dev/null| while read -r linea
do
echo $linea|grep -i "starting full system" >/dev/null
if [ $? -eq 0 ]
then
echo "$TOTAL_UPG $TOTAL_INS $TOTAL_DEL"
break
fi
echo $linea|grep -i "upgraded" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_UPG=`expr $TOTAL_UPG + 1 `
else
echo $linea|grep -i "installed" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_INS=`expr $TOTAL_INS + 1 `
else
echo $linea|grep -i "removed" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_DEL=`expr $TOTAL_DEL + 1 `
fi
fi
fi
done
echo "$TOTAL_UPG $TOTAL_INS $TOTAL_DEL"
I'm trying to parse the pacman.log
When I exec this the exit is 0 0 0, but the lastest value of $TOTAL_UPG was 7.
Edit: Bad example for my problem
Last edited by neurowasho (2008-12-17 23:19:40)
Offline
It works as you expect, change -eq 3 to -eq 1. (the while loop was never entered)
But you're running into this problem elsewhere?
If you get a more complex script it's easy to lose in a subshell, e.g.
#!/bin/sh
foo=1
(while [ $foo -eq 1 ]; do foo=3; done)
echo $foo
Will echo 1
Offline
the syntax of the wile loop is:
while [ the condition is true ]
do
...
done
$foo -eq 3 is false so the loop is not executed.
try something like:
#!/bin/sh
foo=1
while [ $foo -lt 3 ]
do
foo=3
done
echo $foo
Last edited by sisco311 (2008-12-17 21:52:00)
don't drink unwashed fruit juice.
i never make predictions, especially about the future.
Offline
Offline
Thanks for the fast reply, but was a misspelling, the condition was -ne.
The real code is:
TOTAL_UPG=0
TOTAL_INS=0
TOTAL_DEL=0
STRING_UPG=`echo $1|sed -e 's/\[/\\\[/g;s/\]/\\\]/g'`
LINEA_UPG=`grep -n "$STRING_UPG" $PACMANLOG|awk -F: '{print $1}'`
tail -n `expr $NUM_LINES_PACLOG - $LINEA_UPG 2>/dev/null` $PACMANLOG 2>/dev/null| while read -r linea
do
echo $linea|grep -i "starting full system" >/dev/null
if [ $? -eq 0 ]
then
echo "$TOTAL_UPG $TOTAL_INS $TOTAL_DEL"
break
fi
echo $linea|grep -i "upgraded" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_UPG=`expr $TOTAL_UPG + 1 `
else
echo $linea|grep -i "installed" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_INS=`expr $TOTAL_INS + 1 `
else
echo $linea|grep -i "removed" >/dev/null
if [ $? -eq 0 ]
then
TOTAL_DEL=`expr $TOTAL_DEL + 1 `
fi
fi
fi
done
echo "$TOTAL_UPG $TOTAL_INS $TOTAL_DEL"
I'm trying to parse the pacman.log
When I exec this the exit is 0 0 0, but the lastest value of $TOTAL_UPG was 7.
Offline
When you pipe you make a subshell.
This will fail:
#!/bin/sh
foo=1
echo 3 | while read foo
do
echo Foo is read: $foo
done
echo $foo
Please read: http://tldp.org/LDP/abs/html/gotchas.html
In this case, try to change this line:
tail -n `expr $NUM_LINES_PACLOG - $LINEA_UPG 2>/dev/null` $PACMANLOG 2>/dev/null| while read -r linea
To:
IFS="
"
for linea in $(tail -n $(expr $NUM_LINES_PACLOG - $LINEA_UPG 2>/dev/null) $PACMANLOG 2>/dev/null)
EDIT: line->linea in my suggestion
Last edited by Procyon (2008-12-17 23:13:44)
Offline
Thanks a lot
Now it works.
Offline