I've created a bash script for a cron job that I'm using to convert an uploaded file to a couple of formats and then moving it to another folder where another cron job will store it away, etc.

The problem is that I created this with a single folder coming in from a single server, whereas now, I need to reference 5 folders. I'd rather not create 5 versions of this same script that would only be be different in the target folder name, but I can't understand how to reference an inbound parameter.

Also, if there is anyway of error-trapping, that would be very helpful as well....

Thanks, Scott

BTW, this is an Ubuntu 10.04 os.

This is what I'm starting with.


echo "$(date) :: Looking for images convert and copy to ../outbox"

SERVERNUM="$1:-1" # my first attempt at a default value
# folders are 'web-1-photos', web-2-photos, etc.


for currentFile in *;
    if [ -e $currentFile ]; then
        echo "     Converting $currentFile ..."
        # get extension; everything after last '.'
        basename=`basename "$currentFile"`
        extensionless=`basename $currentFile .$ext`

        # convert according to landscape or portrait
        DIMS=`identify "${currentFile}" | awk '{print $3}'`
        WIDTH=`echo "${DIMS}"| cut -dx -f1`
        HEIGHT=`echo "${DIMS}"| cut -dx -f2`

        if [[ ${WIDTH} -gt ${HEIGHT} ]]; then
            # echo "landscape"
            convert -resize "950" -quality 80 $currentFile "$WORK_DIR/small-$extensionless.jpg"
            convert -resize "181" -quality 60 $currentFile "$WORK_DIR/thumb-$extensionless.jpg"
            # echo "portrait"
            convert -resize "x700" -quality 80 $currentFile "$WORK_DIR/small-$extensionless.jpg"
            convert -resize "x157" -quality 60 $currentFile "$WORK_DIR/thumb-$extensionless.jpg"

        # move original file to output directory
        mv $currentFile $WORK_DIR/$currentFile
  • What exactly is the problem you have ? – Iain Oct 13 '11 at 18:48
  • Well, I found this answer on StackOverflow: stackoverflow.com/questions/699576/… – Scott Oct 13 '11 at 18:49
  • The problem was that the FILES variable was erroring out and the script just started looking in its current directory for files to convert and it wiped out my target directories. – Scott Oct 13 '11 at 18:51

Check that "$1" is defined, else abort execution:

test -n "$1" || exit

or error tracking after cd command:

cd "$FILES" || exit

Sorry, i don't understand the "reference inbound parameters", nor why you would need five versions of the script. You are already referencing input parameters, so what's the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.