You are not logged in.

#1 2010-02-28 12:24:05

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,600
Website

critique this bash script - a more elegant way to do it?

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 packagesZsh and other configs

Offline

#2 2010-02-28 12:35:50

Profjim
Member
From: NYC
Registered: 2008-03-24
Posts: 658

Re: critique this bash script - a more elegant way to do it?

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

#3 2010-02-28 13:22:59

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,600
Website

Re: critique this bash script - a more elegant way to do it?

Thanks for the tip... 1048576 then


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#4 2010-02-28 17:16:22

Bralkein
Member
Registered: 2004-10-26
Posts: 354

Re: critique this bash script - a more elegant way to do it?

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 smile

Offline

#5 2010-02-28 18:05:02

hw-tph
Member
Registered: 2006-11-01
Posts: 149

Re: critique this bash script - a more elegant way to do it?

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

Board footer

Powered by FluxBB