Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Bash while loop help

  1. #1
    Join Date
    Dec 2006
    Beans
    181
    Distro
    Ubuntu 7.04 Feisty Fawn

    Exclamation Bash while loop help

    Hi Guys,

    I'm currently working on a simple bash script that will check the current size of files inside directory and if it's larger than specific value .. the script should remove the files until it reached to specific value .. but it seems it's not working "The script always going outside the loop which is weird for me "

    Code:
    #!/bin/bash
    CURR_DIR_SIZE=`du -sk /opt/test/ | cut -f1`
    MAX_SIZE=81920
    
    #echo $CURR_DIR_SIZE
    
    
    while [[ $CURR_DIR_SIZE -gt $MAX_SIZE ]]
    
    do
    echo $CURR_DIR_SIZE
    
    FILE=`ls -1tra test/ | head -1`
    rm -rf $FILE
    
    CURR_DIR_SIZE=`du -sk /opt/test/ | cut -f1`
    done
    exit 0
    Any help

    Thanks in advance
    Last edited by black_ice; December 20th, 2012 at 02:54 PM.
    Knowledge is belong to human

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Bash while loop help

    Did you try with single brackets:

    Code:
    while [ $CURR_DIR_SIZE -gt $MAX_SIZE ]

  3. #3
    Join Date
    Dec 2006
    Beans
    181
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash while loop help

    Thanks for reply back :

    I have tried it, but it's entering endless loop !! without removing any files
    Knowledge is belong to human

  4. #4
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Bash while loop help

    Do you have write permission (and hence remove permission in that directory)? Maybe rm cannot do its job.

    Edit: and are you in the right directory doing it? Maybe you should use absolute path in the FILE= statement

    Code:
    FILE=`ls -1tra /opt/test/ | head -1`
    Code:
    rm -rf /opt/test/$FILE
    Last edited by sudodus; December 20th, 2012 at 03:21 PM. Reason: absolute paths added

  5. #5
    Join Date
    Apr 2011
    Location
    Maryland
    Beans
    1,461
    Distro
    Kubuntu 12.04 Precise Pangolin

    Re: Bash while loop help

    You might consider using stat for this. something along the lines of:

    Code:
    MAX_SIZE=81920
    while [[ $(stat -c%s $CURR_DIR_SIZE) -gt $MAX_SIZE ]]; then
        do ......
    You might play with the formatting of the stat output and make sure that $MAX_SIZE matches the units correctly.

  6. #6
    Join Date
    Dec 2006
    Beans
    181
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash while loop help

    Yes I do, I'm executing the script as a root ..
    Knowledge is belong to human

  7. #7
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Bash while loop help

    I think the solution is absolute paths. See my previous post, it is edited twice.

  8. #8
    Join Date
    Apr 2012
    Beans
    7,068

    Re: Bash while loop help

    I second using 'stat' instead of 'ls'

    You appear to be testing the size of /opt/test but attempting to remove files from the current directory

    You should also quote your $file variable in case it has spaces

    I don't think there is anything wrong with your loop syntax itself (although it would be more idiomatic to use < > operators instead of -lt -gt in an extended test [[ ]] I think)

  9. #9
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Bash while loop help

    Quote Originally Posted by steeldriver View Post
    i second using 'stat' instead of 'ls'

    you appear to be testing the size of /opt/test but attempting to remove files from the current directory

    you should also quote your $file variable in case it has spaces

    i don't think there is anything wrong with your loop syntax itself (although it would be more idiomatic to use < > operators instead of -lt -gt in an extended test [[ ]] i think)
    +1

  10. #10
    Join Date
    Dec 2006
    Beans
    181
    Distro
    Ubuntu 7.04 Feisty Fawn

    Re: Bash while loop help

    Thanks guys for your valuable input, but the script didn't do the desired function , here is the new code

    Code:
    #!/bin/bash
    CURR_DIR_SIZE=`du -sk /opt/test/ | cut -f1`
    MAX_SIZE=81920
    
    #echo $CURR_DIR_SIZE
    
    
    while [[ $CURR_DIR_SIZE > $MAX_SIZE ]]
    do
    #echo $CURR_DIR_SIZE
    
    FILE=`ls -1htr /opt/test/ | head -1`
    rm -rf /opt/test/$FILE
    
    done
    exit 0

    Bash -x

    # bash -x RemoveFiles.sh
    ++ du -sk /opt/test/
    ++ cut -f1
    + CURR_DIR_SIZE=164020
    + MAX_SIZE=81920
    + [[ 164020 > 81920 ]]
    + exit 0
    Knowledge is belong to human

Page 1 of 2 12 LastLast

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
  •