Skip to content

Instantly share code, notes, and snippets.

@qgy18
Created October 10, 2015 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save qgy18/74ef574f89c5aaab253b to your computer and use it in GitHub Desktop.
Save qgy18/74ef574f89c5aaab253b to your computer and use it in GitHub Desktop.
#!/bin/bash
isDir()
{
local dirName=$1
if [ -d $dirName ]; then
echo true
else
echo false
fi
}
isImage()
{
local fileName=$1
if [[ $fileName == *png || $fileName == *jpg ]]; then
echo true
else
echo false
fi
}
isExistWebp()
{
local fileName=$1
fileName="${fileName}.webp"
if [ -f $fileName ]; then
echo true
else
echo false
fi
}
recursionDir()
{
local dir=$1
echo "current dir: ${dir}"
if $(isDir "${dir}")
then :
else
echo "error,please pass a dirctory";
exit 1
fi
local filelist=`ls -tr "${dir}"`
for filename in $filelist
do
local fullpath="${dir}"/"${filename}";
if $(isDir "${fullpath}");then
recursionDir "${fullpath}"
else
if $(isImage "${fullpath}") && ! $(isExistWebp "${fullpath}");then
echo "${fullpath}.webp not exist!"
cwebp "${fullpath}" -o "${fullpath}.webp"
fi
fi
done
}
#usage:
recursionDir "./static"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment