Find the answer to your Linux question:
Results 1 to 4 of 4
hello, i'm trying to write a script to do an action on all, say, *.ext, files, but not if it's name is listed in a file called "excludelist". so I ...
  1. #1
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123

    variable scope / loops?

    hello, i'm trying to write a script to do an action on all, say, *.ext, files, but not if it's name is listed in a file called "excludelist".
    so I have
    Code:
    for i in *.ext; do
    {
     if [ -f $i ] ; then
      includefile=1
      if [ -f excludelist ]; then
        cat excludelist |while read line; do {
          if [ "${line}" == "$i" ]; then
            includefile=0
            break
          fi
        }; done
      fi
      if [ "$includefile" == "1" ] ; then
      #... (do my actions on file)
      fi
    }; done
    but unfortunately, when I set includefile=0, it appears to be creating its own variable of the same name in the scope of the inner loop - as the variable I create by setting includefile=1 before that loop starts isn't being modified!

    How do I modify the existing variable from within the scope of the inner loop?

  2. #2
    Just Joined!
    Join Date
    Jan 2007
    Posts
    90
    use export
    export includefile=0
    will make it global to the script !

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    A pipeline causes new processes to be created, which will use new, different variables. One solution is to avoid pipelines. A short example:
    Code:
    #!/bin/sh
    
    # @(#) s1       Demonstrate new scope for pipeline.
    
    cat >t1 <<EOF
    one
    two
    thr
    EOF
    
    echo
    echo " Situation with piping into loop."
    i=0
    echo " Value of i before loop: $i"
    
    cat t1 |
    while read line
    do
      i=$(( i+1 ))
      echo " Value of i inside loop: $i"
    done
    
    echo
    echo " Value of i after loop: $i"
    
    echo
    echo " Situation with input re-direction:"
    echo
    i=0
    echo " Value of i before loop: $i"
    while read line
    do
      i=$(( i+1 ))
      echo " Value of i inside loop: $i"
    done < t1
    
    echo
    echo " Value of i after loop: $i"
    
    exit 0
    Which produces:
    Code:
    % ./s1
    
     Situation with piping into loop.
     Value of i before loop: 0
     Value of i inside loop: 1
     Value of i inside loop: 2
     Value of i inside loop: 3
    
     Value of i after loop: 0
    
     Situation with input re-direction:
    
     Value of i before loop: 0
     Value of i inside loop: 1
     Value of i inside loop: 2
     Value of i inside loop: 3
    
     Value of i after loop: 3
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  4. #4
    Linux Newbie
    Join Date
    Nov 2006
    Posts
    123
    Quote Originally Posted by drl
    Hi.

    A pipeline causes new processes to be created, which will use new, different variables. One solution is to avoid pipelines. A short example:
    (working example)
    Best wishes ... cheers, drl
    that works perfect, cheers

Posting Permissions

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