Skip to content

Instantly share code, notes, and snippets.

@intsilence
Forked from qgy18/convert_to_webp.sh
Created October 11, 2015 06:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save intsilence/61614424380dcd26e731 to your computer and use it in GitHub Desktop.
Save intsilence/61614424380dcd26e731 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