Created
December 15, 2011 10:32
-
-
Save bak1an/1480649 to your computer and use it in GitHub Desktop.
bash function to watch if file size was changed for some period
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# function takes 2 arguments | |
# $1 - file to watch for changes | |
# $2 - how long to watch in seconds | |
# it echoes "0" if file size was not changed during watching | |
# and "1" if file does not exist or file size was changed | |
# example: | |
# res=$(watch_for_size_change /path/to/file 10) | |
# .... | |
watch_for_size_change() { | |
SLEEP_TIME=1 | |
if [ -e $1 ]; then | |
size=`du -bs $1` | |
count=1 | |
sleep $SLEEP_TIME | |
while [ $count -lt $2 ]; do | |
new_size=`du -bs $1` | |
if [ "$new_size" != "$size" ]; then | |
echo "1"; | |
exit; | |
fi; | |
sleep $SLEEP_TIME | |
let "count+=1" | |
done; | |
echo "0"; | |
else | |
echo "1"; | |
fi; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment