Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

#!/bin/bash

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.
FILES=/home/tech/web-${SERVERNUM}-photos/inbox
WORK_DIR=/home/tech/web-${SERVERNUM}-photos/outbox

cd $FILES

for currentFile in *;
do
    if [ -e $currentFile ]; then
        echo "     Converting $currentFile ..."
        # get extension; everything after last '.'
        ext=${currentFile##*.}
        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"
        else
            # echo "portrait"
            convert -resize "x700" -quality 80 $currentFile "$WORK_DIR/small-$extensionless.jpg"
            convert -resize "x157" -quality 60 $currentFile "$WORK_DIR/thumb-$extensionless.jpg"
        fi

        # move original file to output directory
        mv $currentFile $WORK_DIR/$currentFile
    fi
done
share|improve this question
    
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

1 Answer 1

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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