我为一个cron作业创build了一个bash脚本,用于将上传的文件转换为几种格式,然后将其移动到另一个cron作业存储的另一个文件夹中,等等。
问题是我创build这个来自单个服务器的单个文件夹,而现在,我需要引用5个文件夹。 我宁愿不创build5个版本的这个脚本,只会在目标文件夹名称不同,但我不明白如何引用入站参数。
此外,如果有任何错误陷阱,这将是非常有帮助的….
谢谢,斯科特
顺便说一句,这是一个Ubuntu 10.04操作系统。
这是我开始的。
#!/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
检查是否定义了“$ 1”,否则中止执行:
test -n "$1" || exit
或在cd命令之后跟踪错误:
cd "$FILES" || exit
对不起,我不明白“引用入站参数”,也不知道为什么你需要五个版本的脚本。 您已经参考了input参数,那么问题是什么?