LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-23-2012, 08:01 PM   #1
casperdaghost
Member
 
Registered: Aug 2009
Posts: 349

Rep: Reputation: 16
break out oa while loop once size condition is met


I have this script - it is just a one liner. It lists a directory for 2 incrementing files.
I use it to monitor a file while it increments.

can you see at the bottom where the file is the same size - taht means that the process is done.

Is there a creative way to break out of the while loop when the files size is stopped incrementing and send em an email or an xmessage.

It does not have to be a one liner - i could make a file .



casper@casperbix $ while true ; do ls -ltr /data/tag/data/ | grep BX | grep 02012012; sleep 10 ; done

Code:
casper@casperbix $ while true ; do  ls -ltr /data/tag/data/ | grep BX | grep 02012012; sleep 10 ; done


-rw-r--r-- 1 root root         10 Feb 23 18:58 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root         10 Feb 23 18:58 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root    1059020 Feb 23 18:58 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root    4130868 Feb 23 18:58 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root    3455012 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   12168014 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root    5637960 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   20862706 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root    6453164 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   24012268 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root    9163164 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   29348288 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   14049940 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   40194424 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   19346840 Feb 23 18:59 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   49824924 Feb 23 18:59 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   25234606 Feb 23 19:00 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   60753350 Feb 23 19:00 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   25234606 Feb 23 19:00 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   60753350 Feb 23 19:00 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   25234606 Feb 23 19:00 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   60753350 Feb 23 19:00 02012012-BX-LISTED.gz
-rw-r--r-- 1 root root   25234606 Feb 23 19:00 02012012-BX-ENGLAND.gz
-rw-r--r-- 1 root root   60753350 Feb 23 19:00 02012012-BX-LISTED.gz

~
 
Old 02-23-2012, 09:02 PM   #2
savona
Member
 
Registered: Mar 2011
Location: Bellmawr, NJ
Distribution: Red Hat / Fedora
Posts: 195

Rep: Reputation: 50
I am not the best script writer as you will see, but try this:

MAXSIZE="60753350";CURSIZE=0; while [ "$CURSIZE" -lt "$MAXSIZE" ] ; do LIST=`ls -ltr /data/tag/data/ | grep BX | grep 02012012`; echo $LIST; echo -ne "CURRENT FILE SIZE:"; CURSIZE=`ls -ltr /data/tag/data/ | grep BX | grep 02012012 | awk '{print $5}'`; echo $CURSIZE; sleep 10 ; done
 
Old 02-23-2012, 10:10 PM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Something like (not tested)
Code:
#!/bin/bash

files=( echo /data/tag/data/02012012-BX* )
for (( i=0; i<$(#files[*]}; i++ ))
do
    sizes[ i ]=-1
done

while true
do
    ls -ltr /data/tag/data/02012012-BX*
    sleep 10
    incrementing=false
    for (( i=0; i<$(#files[*]}; i++ ))
    do
        old_sizes[i]=${sizes[ i ]}
        sizes[i]=$( stat --format=%s "${files[ i ]}" )
        [[ ${sizes[i]} -ne ${old_sizes[i]} ]] && incrementing=true
    done
    [[ $incrementing = false ]] && break
done
 
Old 02-23-2012, 10:14 PM   #4
Geek255
Member
 
Registered: Feb 2012
Posts: 41

Rep: Reputation: Disabled
To receive a x message after the loop is done add one of these after done.

xmessage -nearmouse Done
gdialog --msgbox Done
kdialog --msgbox Done

You can replace Done with any text. If your message has spaces put the text in quotes. eg
"All Done!"

Last edited by Geek255; 02-23-2012 at 10:20 PM.
 
Old 02-23-2012, 10:35 PM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,576
Blog Entries: 31

Rep: Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195Reputation: 1195
Ooops -- missed the xmessage and email part.

@casperdaghost: where do you want to send the email? On the local machine or to the Internet? If the Internet, do you have a mail transfer agent running on the computer, such as sendmail, postfix or exim?
 
Old 02-23-2012, 11:20 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,251

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I made a slight variation on catkin's as the sizes of the files may not grow at the same pace and hence you do not want the last file checked to rule the outcome:

(This is also untested)
Code:
#!/bin/bash

declare -A size

for f in /data/tag/data/02012012-BX*
do
    size[$f]=-1
done

nsize=0

while true
do
    sleep 10
    for f in ${!size[@]}
    do
        size[$f]=nsize
        nsize=$(stat -c%s $f)
        if (( nsize == ${size[$f]} ))
        then
            stop=true
        else
            stop=false
            break
        fi
    done
    [[ $stop ]] && break
done
I did not bother with the message part either as I figure you know how to do that

Last edited by grail; 02-24-2012 at 02:54 AM.
 
Old 02-24-2012, 02:11 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
Try this, it monitors the directory and once the incrementing has stopped, it sends you a mail

Code:
#!/bin/bash

while true;
do

MAXENGLAND=$(ls -ltr /data/tag/data/ | grep -E 'BX-ENGLAND.gz' | awk '{print $5}' | tail -1)
PREVIOUSENGLAND=$(ls -ltr /data/tag/data/ | grep -E 'BX-ENGLAND.gz' | awk '{print $5}' | tail -2 | head -1)
MAXLISTED=$(ls -ltr /data/tag/data/ | grep -E 'BX-LISTED.gz' | awk '{print $5}' | tail -1)
PREVIOUSLISTED=$(ls -ltr /data/tag/data/ | grep -E 'BX-LISTED.gz' | awk '{print $5}' | tail -2 | head -1)

        if [ $MAXENGLAND -eq $PREVIOUSENGLAND && $MAXLISTED -eq $PREVIOUSLISTED ]
        then

       echo "All is done" | mail -s "Increment completion" administrator@gmail.com
        exit
else

         sleep 10
        echo Running again
        fi
done

Last edited by cbtshare; 02-24-2012 at 02:15 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Bash script to replace line break with condition spart1985 Linux - Newbie 8 02-23-2012 10:43 AM
[SOLVED] Condition loop. Sha_unix Linux - Newbie 10 11-30-2011 01:55 AM
[SOLVED] Shell script: condition always met ? x111 Linux - Newbie 4 09-24-2010 06:27 PM
while () not exiting even when exit condition is met aatwell Programming 28 09-15-2008 08:49 PM
Cannot catch SIGINT while serial break condition occurs gzz Programming 1 09-17-2007 02:19 AM


All times are GMT -5. The time now is 09:10 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration