最近学习linux shell脚本,尝试写了个uglifyjs的批处理压缩脚本。写的不好,请各位大大们多多指教。
环境: ubuntu 12.10 64bit./ bash/ node 0.8.15/ uglifyjs 2
Shell脚本如下:
#!/bin/sh
#
# This file is used to compress js files.
# create by Allen Heavey. @DMDGeeker
# 2013/02/01
#
#
function compressJS() {
uglifyjs $1 -c -m -o $1
}
function compress() {
local par=$1
local split="/"
if [ -f $par ] && [ -r $par ] && [ "${par##*.}" == "js" ]; then
compressJS $par
elif [ -d $par ]; then
for i in `ls $par`
do
local j=${par}${split}${i}
#test -d $j && echo "dir $j " || echo "file $j"
compress $j
unset j
done
unset i
fi
}
for var in $@
do
path=${var}
compress $path
done
使用方法:
保存脚本(如js.sh)后, 使用类似于 bash js.sh XXXX(可以为目录或文件).
这样可以一次批处理多个JS文件,简化操作。