Results 1 to 6 of 6

Thread: Changing a var in a shell loop

  1. #1
    Join Date
    Nov 2009
    Beans
    4

    Changing a var in a shell loop

    Can someone explain to me why $text is blank after exiting the loop?

    Code:
    #!/bin/bash
    BOX='my.box'
    THRESHOLD=10
    MAILADDRESS='foo.bar@foo.com'
    text=''
    df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;
    do
        usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
        partition=$(echo $output | awk '{ print $2 }' )
        if [ $usep -ge $THRESHOLD ]; then
            text="${text}Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)\n"
        fi
        echo ${#text}
    done
    echo $text

  2. #2
    Join Date
    May 2007
    Location
    I really do not know.
    Beans
    130
    Distro
    Ubuntu 9.04 Jaunty Jackalope

    Re: Changing a var in a shell loop

    It could be because the text inside the loop is a local variable.
    The road not yet taken.

  3. #3
    Join Date
    Nov 2009
    Beans
    4

    Re: Changing a var in a shell loop

    Okay so how do I get it to change the var that I assigned outside of the loop, or access this var outside of the loop?

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: Changing a var in a shell loop

    you can see this similar example

  5. #5
    Join Date
    Mar 2009
    Beans
    927
    Distro
    Ubuntu 12.04 Precise Pangolin

    Lightbulb While Statement is Odd

    Quote Originally Posted by 0cton View Post
    It could be because the text inside the loop is a local variable.
    I'm pretty sure it's nothing like that, I think it's because you're while statement is odd:
    Code:
    df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output;

    This seems to work for me:
    [BASH]
    PHP Code:
    #!/bin/bash
    THRESHOLD=10
    text
    =''
    OLD_IFS=${IFS}
    IFS='
    '
    for output in `df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{print $5 " " $1}'`
    do
        
    size=`echo ${output} | awk '{ print $1}' | cut -d'%' -f1`
        
    partition=$(echo ${output} | awk '{ print $2 }' )
        if [ ${
    size} -ge ${THRESHOLD} ]
        
    then
            text
    ="${text}Running out of space on \"${partition}\" (${size}% full).\n"
        
    fi
    done
    IFS
    =${OLD_IFS}
    text="${text}\t-- `hostname`: `date`\n"
    echo -ne ${text
    (we change the IFS so that the for loop runs line-by-line rather than word-by-word)
    Last edited by Penguin Guy; November 11th, 2009 at 06:42 PM.

  6. #6
    Join Date
    Nov 2009
    Beans
    4

    Re: Changing a var in a shell loop

    Thanks for all the help people, I almost had kittens at the explanation of the while loop running a subshell, never knew that and think its a bit mad. Both the awk and shell response work nicely, thanks

Bookmarks

Posting Permissions

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