You are not logged in.
This script cleans up a user's firefox .sqlite files and reports the savings in Mbytes. It's rather basic as you can see. I'm looking for suggestions to make it more elegant.
#!/bin/bash
path=/dev/shm/profile-firefox
for dbs in $(find ${path} -name '*.sqlite'); do
bsize=`ls -l $dbs | awk {'print $5'}`
sqlite3 $dbs vacuum
sqlite3 $dbs reindex
asize=`ls -l $dbs | awk {'print $5'}`
dsize=`echo $bsize - $asize | bc`
dsizeM=`echo "scale=2; $dsize / 1024000" | bc`
echo $dbs reduced by $dsizeM Mbytes
done
Last edited by graysky (2010-02-28 12:24:19)
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
You could do some of the math in bash, but not the floating point math:
dsize=$((bsize-asize))
Or just spawn a single bc process:
dsizeM=$(echo "scale=2; ($bsize-$asize)/1024000" | bc)
Also, why are you using 1024000? See here.
Other than that, it looks fine.
Offline
Thanks for the tip... 1048576 then
CPU-optimized Linux-ck packages @ Repo-ck • AUR packages • Zsh and other configs
Offline
Apart from the issue already mentioned, I think it looks fine. Bash scripting is a world of nasty string mangling anyway so IMO elegance is not a primary concern, unless you're making a big long script and you need to keep things tidy. For small scripts like yours, I'd just concentrate on getting things done. Maybe put a comment at the top describing the purpose of the script in case you come across it 5 years later and wonder why the heck you wrote it (happens to me a lot!).
One very tiny criticism I would make (mainly a matter of taste) is how you write your awk statement. If you put the single quotes at the beginning/end of the statement, instead of inside the braces, then the style will be more consistent for more complex statements:
awk '{print $5}'
awk '$5 ~ /foo/ {print $5}'
Also if you put the braces first it looks a bit like you're trying to do brace expansion. Well maybe that's all a bit of a silly nitpick but TBH it's the only other thing I could think of
Offline
I would like to add that new style command substitution is far more readable when it comes to complex statements compared to the old style with backticks that you use in your script.
# Old style command substitution
bsize=`ls -l $dbs | awk {'print $5'}`
# New style command substitution
bsize=$(ls -l $dbs | awk {'print $5'})
Again, this is a matter of personal preference.
Offline