I recently would like to write a shell script that
Runs in the background (can be done with "&", but i'd be happy for other solutions to keep programs running)
Does its stuff
3 THEN checks a specified folder for a size change over time (say, each 5 seconds. AND ONLY continues with the next line in the script when said folders sizes doesn't change anymore.
#!/bin/sh
DIR=~/'unix.com'
dir_size=0;
get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';}
while [ : ]; do
# Script does its stuff
sleep 5
while [ "$(get_size "$DIR")" -ne "$dir_size" ]; do
dir_size=$(get_size "$DIR")
sleep 5
done
echo "Size has not changed since the last 5 seconds. Loop to the top"
done
exit 0
Save this as 'script.sh' or something, and run it with ./script.sh &
If I add something between the exit 0 and done, will that be executed after yhe size check has finished ?
Also, I'd prefer to have "the stuff that has to be done by the script" to be only executed once, and before that loop even starts, then loop till the sizechange isn't happenig anymore and after that continue with whatever I might still want.
Could this possibly be done in more compact form (one-liner lol ?)
#!/bin/sh
DIR=~/'unix.com'
get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';sleep 5;}
# Script does its stuff
dir_size=$(get_size "$DIR")
while [ "$(get_size "$DIR")" -ne "$dir_size" ]; do
echo 'Size has changed. Sleeping for 5 more seconds'
dir_size=$(get_size "$DIR")
done
echo 'Size has not changed since the last 5 seconds.'
#Do other stuff
exit 0
Yes, unless, for example, you're talking about /var/mobile in the system root, in which case you'll issue DIR='/var/mobile' , as ~ denotes your Home directory
You can write this script in a new script, of course:
(Call it for this example "pause.sh")
#!/bin/sh
get_size(){ du -s "$1" 2>/dev/null | awk '$0=$1';sleep 5;}
dir_size=$(get_size "$1")
while [ "$(get_size "$1")" -ne "$dir_size" ]; do
echo 'Size has changed. Pausing 5 more seconds'
dir_size=$(get_size "$1")
done
echo 'Size has not changed since the last 5 seconds.'
return 0