#!/bin/bash NEW_TYPE="png" TYPE_ARR=("jp*g" "tif" "webp" "png") file="" folder="" usage(){ printf "Usage: -f \tConvert single File -d \tConvert all files in Dir -t \tSet new Type -h\t\tDisplay this\n" } img_convert() { IMG="$1" IMG_NEW="$IMG" for type in ${TYPE_ARR[@]}; do IMG_NEW="$(echo ${IMG_NEW} | sed 's/\.'"${type}"'/.'"${NEW_TYPE}"'/g')" done if [ ! -f "${IMG}" ]; then echo "${IMG} not found" return fi if [ "${IMG}" == "${IMG_NEW}" ]; then echo "${IMG} and ${IMG_NEW} are the same" return fi echo "Convert ${IMG} ${IMG_NEW}" convert -quiet "${IMG}" "${IMG_NEW}" >> /dev/null rm "${IMG}" } folder_magic(){ IMG_FOLDER="$1" echo "Convert Imgs in ${IMG_FOLDER}" IMG_ARR=() IMG_ARR_NEW=() IFS=$'\n'; set -f for type in ${TYPE_ARR[@]}; do if [[ "${type}" != "${NEW_TYPE}" ]]; then IMG_ARR+=($(find ${IMG_FOLDER} -name "*${type}")) IMG_ARR_NEW+=($(find ${IMG_FOLDER} -name "*${type}" | sed 's/\.'"${type}"'/.'"${NEW_TYPE}"'/g')) fi done for ((i=0;i<${#IMG_ARR[@]};i++)); do echo "Convert ${IMG_ARR[$i]} ${IMG_ARR_NEW[$i]}" convert -quiet "${IMG_ARR[$i]}" "${IMG_ARR_NEW[$i]}" >> /dev/null rm "${IMG_ARR[$i]}" done } get_config() { if [ $# -eq 0 ] || [ ${1:0:1} != - ]; then echo "Too few Arguments" usage exit fi while [[ ${1:0:1} == - ]]; do echo "Param: $1" [[ $1 == -h ]] && { usage exit }; [[ $1 == -f ]] && { if [ $# -lt 2 ]; then echo "Too few Arguments" usage exit fi file="$2"; shift 2; continue; } [[ $1 == -d ]] && { if [ $# -lt 2 ]; then echo "Too few Arguments" usage exit fi folder="$2"; shift 2; continue; } [[ $1 == -t ]] && { if [ $# -lt 2 ]; then echo "Too few Arguments" usage exit fi NEW_TYPE="$2"; shift 2; continue; } echo "Parameter $1 discarded" shift 1; done } get_config "$@" if [[ "${file}" != "" ]]; then img_convert "${file}" fi if [[ "${folder}" != "" ]]; then folder_magic "${folder}" fi