Shell脚本使用TinyPng批量压缩图片

使用shell脚本,接入tinypng官方程序接口,批量压缩图片

#!/bin/bash
dir=/Users/shake/Documents/tinypng/retry1
dir2=/Users/shake/Documents/tinypng/target2
echo Enter $dir
cd $dir
mkdir -p $dir2
travel_file()
{
    echo $1    
    mkdir -p $2
    for file in `ls $1`
    do
        if [ -f "$1/$file" ]; then
            echo $1/$file
            if [ ${file##*.} == "png" ] || [ ${file##*.} == "PNG" ]; then
                for i in {1..100}
                do
                    echo upload$i $1/$file
                    url=$(cat $1/$file | curl -i --user api:NCNsuEsbrM3tFqR4XP1Urm_Yw-E1e-em --data-binary @- https://api.tinypng.com/shrink --connect-timeout 10 -m 2000 | grep "Location" | cut -c 11-)
                    if [ -n "$url" ]; then
                        break
                    fi
                done

                for i in {1..100}
                do
                    echo getlength$i $url
                    length=$(curl --head $url --connect-timeout 10 -m 30 | grep "Content-Length" | cut -c 17-)
                    if [ -n "$length" ]; then
                        length=${length:0:${#length}-1}
                        echo length ${#length} $length
                        break
                    fi
                done

                for i in {1..100}
                do
                    echo download$i $url                    
                    curl $url > $2/$file --connect-timeout 10 -m 400
                    len=$(ls -l $2/$file | awk '{print $5}')
                    echo len ${#len} $len
                    echo length ${#length} $length
                    if [ $len == $length ]; then
                        echo success! $file
                        break
                    fi
                done
            fi
        fi
        if [ -d "$1/$file" ]; then
            travel_file $1/$file $2/$file
        fi
    done

}


travel_file $dir $dir2


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值