shell脚本实践

#!/bin/sh
suffix=("png" "doc" "html");

for s in ${suffix[@]}; do
    echo $s;
done
while getopts "d:f:h" opt; do
    case $opt in
        d) 
            par=$OPTARG
         ;;
        f)
            log=./$OPTARG
         ;;
        h)
         echo "Help:\n -d: destination directory \n -f: log file"
         exit 22
         ;;
        \?)
         echo "Invalid Input opts";
         exit 22;
         ;;
    esac
done
if [ ! $par ];then
    par=.;
fi
if [ ! $log ];then
    log=./organize.log;
fi
########################
if [ ! -d $par ];then
    mkdir $par;
fi
for s in ${suffix[@]}; do
    echo $par/$s;
    if [ ! -d $par/$s ];then
        mkdir $par/$s;
    fi
done
if [ ! -f $log ];then
    touch $log;
fi
for file in ./*
do
    for s in ${suffix[@]}; do
        if [ "${file##*.}" == $s ];then
            echo "moving $file--";
            mv -i $file $par/$s;
            echo "done";
            echo "move $file to $par/$s on "$(date +%Y"."%m"."%d" "%H":"%M":"%S ) >> $log;
        fi
    done
done
echo "Task Done";

你可能感兴趣的:(shell,脚本)