PDA

View Full Version : Bash while loop help



black_ice
December 20th, 2012, 02:33 PM
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 "



#!/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

sudodus
December 20th, 2012, 03:10 PM
Did you try with single brackets:


while [ $CURR_DIR_SIZE -gt $MAX_SIZE ]

black_ice
December 20th, 2012, 03:12 PM
Thanks for reply back :

I have tried it, but it's entering endless loop !! without removing any files

sudodus
December 20th, 2012, 03:16 PM
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


FILE=`ls -1tra /opt/test/ | head -1`

rm -rf /opt/test/$FILE

drmrgd
December 20th, 2012, 03:18 PM
You might consider using stat for this. something along the lines of:



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.

black_ice
December 20th, 2012, 03:19 PM
Yes I do, I'm executing the script as a root ..

sudodus
December 20th, 2012, 03:23 PM
I think the solution is absolute paths. See my previous post, it is edited twice.

steeldriver
December 20th, 2012, 03:24 PM
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)

sudodus
December 20th, 2012, 03:29 PM
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

black_ice
December 20th, 2012, 03:40 PM
Thanks guys for your valuable input, but the script didn't do the desired function , here is the new 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

rnerwein
December 20th, 2012, 05:29 PM
Thanks guys for your valuable input, but the script didn't do the desired function , here is the new 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
hi
a directory is just a file. and i think that the syscall using TRUNCATE is not used. that means the size "CURR_DIR_SIZE never changed. you can only change it by deleting files
than move the files to a directory e.g. mv * ../blablu
then delete the orginal directory entry, make it again and move the files back.
the directory (it a file) is grown page-size as i know.
bad trap ?
cheers

black_ice
December 20th, 2012, 06:32 PM
Pardon me, But I didn't get it .. it would be nice if you can elaborate more ..

Thanks

sudodus
December 20th, 2012, 07:54 PM
This script works for me



#!/bin/bash

DIRECTORY="/home/$USER/test/black_ice/sub"
MAX_SIZE=81920
CURR_DIR_SIZE=`du -sk "$DIRECTORY" | cut -f1`

#echo $CURR_DIR_SIZE

while [ $CURR_DIR_SIZE -gt $MAX_SIZE ]
do
echo $CURR_DIR_SIZE
FILE=`ls -1htr "$DIRECTORY" | head -1`
rm -rf "$DIRECTORY"/"$FILE"
# echo $?
CURR_DIR_SIZE=`du -sk "$DIRECTORY" | cut -f1`
done
echo "final dir size: $CURR_DIR_SIZE"
exit 0


What you need is to adjust the directory.

black_ice
December 20th, 2012, 08:30 PM
you really offered a great help thanks , the script finally executed without any errors but the script deleted all files which were (161 M) under test directory ... What I need to do is stop deleting files when the directory size is less than 80 M or less

sudodus
December 20th, 2012, 08:44 PM
It worked for me, stopping when the content of the directory was less than MAX_SIZE. But of course, if the last remaining file is too big, it will also be deleted. Try with files smaller than MAX_SIZE!

rnerwein
December 20th, 2012, 08:54 PM
Pardon me, But I didn't get it .. it would be nice if you can elaborate more ..

Thanks
hi
ok give me another try.
1. mkdir tst (in your home)
2. cd tst
3. mkdir tmp
4. du -sk tmp ----> 4 tmp (initial size for a directory
5. cd tmp
6. vi bla.sh
a=1
while :; do touch bla$a; ((a++)); echo $a; done
save that script
7. at, lets say at 5000 hit CTRL C to stop that loop
8. cd ..
9. du -sk ./tmp ----> 136 tmp
in your scrip you say: MAX_SIZE=130
e.g. CURR_DIR_SIZE= 120
the script will never run: 120 > 130
10. cd tmp
11. rm bla*
12. cd ..
13. du -sk ./tmp ----> 136 tmp as you see even if your script will run (CUR.. > MAX.
it will run forever even all the files bla* deletet the directory have the same size.
atention ! the file is not deleted only the inode entry is. the file is still on disk but the former disk block of the file is added to the free list and the
system don't shrink the directory (wich is only a file in sense of unix)

did we come together now ?
cheers

black_ice
December 20th, 2012, 11:09 PM
@sudodus, You are right thanks for your dedication and your fast responses .. My Problem Solved

@rnerwein, This is super . Appreciate your detailed steps it helped me getting the full picture ..

Thanks Guys :)