From c8bc9ae00baac62ffa648b63c5b8b8de2f791beb Mon Sep 17 00:00:00 2001 From: VirtuBox Date: Wed, 20 Mar 2019 01:38:32 +0100 Subject: [PATCH] set image path as optional --- README.md | 14 +++++++------- optimize.sh | 53 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 9b7e4aa..0e955d5 100644 --- a/README.md +++ b/README.md @@ -40,19 +40,19 @@ source $HOME/.bashrc ### Usage ```bash -Usage: img-optimize [options] +Usage: img-optimize [options] +If images path is not defined, img-optimize will use the current directory Options: - --jpg ..... optimize all jpg images - --png ..... optimize all png images - --webp ..... convert all images in webp - --nowebp ..... optimize all png & jpg images - --all ..... optimize all images (png + jpg + webp) + --jpg ..... optimize all jpg images + --png ..... optimize all png images + --webp ..... convert all images in webp + --nowebp ..... optimize all png & jpg images + --all ..... optimize all images (png + jpg + webp) Other options : -h, --help, help ... displays this help information Examples: optimize all jpg images in /var/www/images img-optimize --jpg /var/www/images - ``` ### Update the script diff --git a/optimize.sh b/optimize.sh index 9ab1666..d578923 100755 --- a/optimize.sh +++ b/optimize.sh @@ -18,13 +18,14 @@ CGREEN="${CSI}1;32m" _help() { echo "Bash script to optimize your images and convert them in WebP " - echo "Usage: img-optimize [options] " + echo "Usage: img-optimize [options] " + echo "If images path isn't defined, img-optimize will use the current directory " echo " Options:" - echo " --jpg ..... optimize all jpg images" - echo " --png ..... optimize all png images" - echo " --webp ..... convert all images in webp" - echo " --nowebp ..... optimize all png & jpg images" - echo " --all ..... optimize all images (png + jpg + webp)" + echo " --jpg ..... optimize all jpg images" + echo " --png ..... optimize all png images" + echo " --webp ..... convert all images in webp" + echo " --nowebp ..... optimize all png & jpg images" + echo " --all ..... optimize all images (png + jpg + webp)" echo " Other options :" echo " -h, --help, help ... displays this help information" echo "Examples:" @@ -43,38 +44,56 @@ if [ "${#}" = "0" ]; then exit 1 else - while [ ${#} -gt 0 ]; do - case "${1}" in + while [ "$#" -gt 0 ]; do + case "$1" in --jpg) JPG_OPTIMIZATION="y" if [ "$2" ]; then IMG_PATH=$2 + shift + else + IMG_PATH="./*" fi - shift ;; --png) PNG_OPTIMIZATION="y" - IMG_PATH=$2 - shift + if [ "$2" ]; then + IMG_PATH=$2 + shift + else + IMG_PATH="./*" + fi ;; --nowebp) JPG_OPTIMIZATION="y" PNG_OPTIMIZATION="y" WEBP_OPTIMIZATION="n" - IMG_PATH=$2 - shift + if [ "$2" ]; then + IMG_PATH=$2 + shift + else + IMG_PATH="./*" + fi ;; --webp) WEBP_OPTIMIZATION="y" - IMG_PATH=$2 - shift + if [ "$2" ]; then + IMG_PATH=$2 + shift + else + IMG_PATH="./*" + fi ;; --all) PNG_OPTIMIZATION="y" JPG_OPTIMIZATION="y" WEBP_OPTIMIZATION="y" - IMG_PATH=$2 - shift + if [ "$2" ]; then + IMG_PATH=$2 + shift + else + IMG_PATH="./*" + fi ;; -h | --help | help) _help