Bash Scripts

I wrote a neat little bash script that would allow me to resize jpg images to a specified width. This is useful for reducing digital camera photos to a manageable size for uploading and sharing on the web. Yes, the gallery does automatically generate a 640×480 pictures, but it is ludicrous for me to upload 100s of 2MB digital images when my server space and bandwidth are free 🙂

It resizes to 640 width automatically unless an optional argument is supplied to set a different size constraint.

#!/bin/bashsize=640

if [ “$1” != “” ]; then
size=$1
fi

#Find .jpg, .JPG, and any combination thereof
for file in *.[jJ][pP][gG]
do
convert -resize ${size}x $file output_$file
done

Also on the subject of bash scripts, I found the following script here that seems to work pretty well for basic file class renaming. The usage of this script requires three arguments. The first argument is the criteria you want to match (this cannot include any globbing expansions that will find more than one file), the second argument is what you want to replace, and the third argument is what you want it to replace it with. My one problem with this script is that the first and second arguments are almost always the same for me since all the files I want to change match the prescribed pattern. I also wish it could handle globbing statements. This may be possible if we set the last argument to be what we want to match. In that case we could just loop on that instead of doing an ls in the script. Anyway, for now this is an adequate script.

#!/bin/bash
criteria=$1
re_match=$2
replace=$3

for i in $( ls *$criteria* );
do
src=$i
tgt=$(echo $i | sed -e “s/$re_match/$replace/”)
mv $src $tgt
done

I considered tagging this as hacks too because there is not proper error checking on the image script to make certain you enter integers only. I bet I could add that pretty easily. Anyway, one last thought, what percentage of bash scripts do you guys suppose are ‘hacks’ in the sense that they are quick-and-dirty without all the proper error checking that you ought to have if you were distributing this for-real? I’d bet 90+%, especially one-shot scripts.