You are not logged in.

#1 2008-12-17 21:34:05

neurowasho
Member
Registered: 2007-12-28
Posts: 4

[SOLVED]Global variable in SH script

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 sad

Last edited by neurowasho (2008-12-17 23:19:40)

Offline

#2 2008-12-17 21:49:42

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED]Global variable in SH script

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

#3 2008-12-17 21:51:19

sisco311
Member
From: Romania
Registered: 2008-05-23
Posts: 112

Re: [SOLVED]Global variable in SH script

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

#4 2008-12-17 21:55:15

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: [SOLVED]Global variable in SH script

check here

your basically saying:

foo is 1 [foo is 1]

while foo is equal to 3 make foo 3 [foo is still 1, not 3; so foo is still 1]

what is foo? [foo is 1]

change -eq to -lt (less than)

edit... too slow

Last edited by brisbin33 (2008-12-17 21:55:39)

Offline

#5 2008-12-17 22:56:53

neurowasho
Member
Registered: 2007-12-28
Posts: 4

Re: [SOLVED]Global variable in SH script

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

#6 2008-12-17 23:13:00

Procyon
Member
Registered: 2008-05-07
Posts: 1,819

Re: [SOLVED]Global variable in SH script

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

#7 2008-12-17 23:19:12

neurowasho
Member
Registered: 2007-12-28
Posts: 4

Re: [SOLVED]Global variable in SH script

Thanks a lot smile

Now it works.

Offline

Board footer

Powered by FluxBB