Find the answer to your Linux question:
Results 1 to 6 of 6
Hi, I'm facing a problem with scope of var: $total in the following script: cat samplefile | cut -d " " -f 4 | cut -d : -f 3 | ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    13

    Scope of variable

    Hi,

    I'm facing a problem with scope of var: $total in the following script:

    cat samplefile | cut -d " " -f 4 | cut -d : -f 3 | while read num
    do
    if [ $n -eq 0 ]
    then
    temp=`expr 60 - $num`
    n=`expr $n + 1`
    else
    temp=`expr $num + $temp`
    n=0
    fi
    echo " Total = $temp"
    done

    echo "Total= $temp"

    The sample file contains the following:

    Tue Dec 25 11:10:25 IST 2007
    Tue Dec 25 12:40:07 IST 2007

    The "echo" within while gives the exact value 42, but outside "echo" gives 0.

    After googling I got that bash creates a subshell after piping, therfore the variables will not hold the values outside while loop.

    Can any one tell me how to resolve this issue i.e. I need the value of $total for further calculation.

    - Sangamesh

  2. #2
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    This is easy. We just obtain the values inside the loop:
    Code:
    ...
    exec 3< samplefile
    ...
    
    while read line <&3
    do
        num=$(echo "$line" | cut -d' ' -f4 | cut -d: -f3)
        if [ $n -eq 0 ]
        then
            temp=`expr 60 - $num`
            n=`expr $n + 1`
        else
            temp=`expr $num + $temp`
            n=0
        fi
        echo " Total = $temp"
    done
    
    echo "Total= $temp"
    Changed lines are bolded.

    Basically, we open up samplefile, and then process each line within the loop, rather than each number. This way, we are able to avoid piping into the loop.
    DISTRO=Arch
    Registered Linux User #388732

  3. #3
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Cabhan's idea will almost certainly work. I wanted to present an alternative idea, though. I've learned from Cabhan's response, and it is my hope that at least sangamesh.bc would learn from both his response and what I'd like to write.

    But I took a closer look at the script and data. sangamesh.bc, you have a whole boatload of problems here.

    When I run the script as you post it, I get this output. (All reddifying in this post is mine.)
    Code:
    ./1.sh: line 3: [: -eq: unary operator expected
    expr: syntax error
     Total = 
     Total = 53
    Total=
    I know how to fix those errors, and I also know how to answer your original question (and that question/problem would remain after you fix all error messages).

    But fixing the error messages is your job; partly because when you present a clean script on this forum you're being courteous to those who wish to help you, and partly because I have no guarantee that my way of "fixing" the errors is in line with what you want done.

    So clean it up, and let's have another go at it. And if you have difficulty cleaning it up, post what you have and we'll help you. Ok?
    --
    Bill

    Old age and treachery will overcome youth and skill.

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Posts
    13
    Hi,

    The script I had posted was with no errors in my premises. I don't know why its giving error in your end. I also faced such problem long before. There were two files with identical content. One file was executing but the other file producing an error wrt "expr". my coleague guessed that file might be corrupted.

    Let it be.

    With Cabhan's idea i got the solution, but blindly.

    I'm not getting the following, which are in bold letters:

    exec 3< samplefile

    while read line <&3

    What does these mean?

    - sangamesh

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    wje, I believe that the entire file was not posted, and therefore $n is uninitialized in your copy, hence the binary operator error.

    However, sangamesh, wje makes a good point. Whenever you use a variable in Bash, you should quote it. For instance, imagine we have the following:
    Code:
    #!/bin/bash
    
    if [ $n -eq 0 ]; then
        echo "It is 0!"
    fi
    This code will throw an error that "-eq" expects two operands. This is because Bash is extremely stupid, and after evaluating the variables, ends up with:
    Code:
    #!/bin/bash
    
    if [ -eq 0 ]; then
        echo "It is 0!"
    fi
    The "$n" is replaced with its value, which is nothingness. If it was quoted (that is: |"$n"|), then the quotes would still exist, and therefore "-eq" has the two operands.

    Now then, onto your questions. From the exec man page:
    Code:
    DESCRIPTION
           The exec utility shall open, close, and/or  copy  file  descriptors  as
           specified by any redirections as part of the command.
    What this does is to open the given file and assign it the given file descriptor. Every program already has 3 file descriptors: 0 is standard input, 1 is standard output, and 2 is standard error. I am opening 3 for input (hence the '<'), and opening a connection to the file called samplefile.

    Having opened this file, in the second instance, I am telling read to read from file descriptor 3 instead of the default (standard input, or file descriptor 0). Therefore, it reads the file as opposed to the terminal.

    This link gives a number of examples and explains it all very well:
    http://www.tldp.org/LDP/abs/html/io-redirection.html
    DISTRO=Arch
    Registered Linux User #388732

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    wje, I believe that the entire file was not posted, and therefore $n is uninitialized in your copy, hence the binary operator error.
    Hmm. I had guessed that the error was that he had meant n and num to be the same variable.

    This reminds me of my earliest days using FORTRAN (with those noisy IBM 026 keypunches; no lower case, etc.), when one of my fellow students said, "I just got a syntax error in my FORTRAN program because I forgot to put a right parenthesis at the end of a statement. Why didn't the compiler just put one there and continue?" And I had to answer, "How would the compiler know that the end of the statement was the right place to put it?"

    That's why I usually don't try to make such a guess public, but get all grouchy (old age permits one) and invite the original poster to correct his script and repost it.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...