Bash 检查是否使用optipng优化图像

Bash 检查是否使用optipng优化图像,bash,shell,Bash,Shell,我使用optipng来优化我项目的图像。 我想通过检查图像是否未优化以运行脚本来自动执行此操作 if [[ $FILE == *.png ]] then BASEFILE=$(basename $FILE) optipng -simulate -quiet $FILE -log $$.log TEST=$(cat $$.log |grep "optimized" |wc -l) .... 问题是我无法将输出写入新文件,我需要在不创建文件的情况下进行检查。

我使用optipng来优化我项目的图像。 我想通过检查图像是否未优化以运行脚本来自动执行此操作

if [[ $FILE == *.png ]]
    then
    BASEFILE=$(basename $FILE)
    optipng -simulate -quiet $FILE -log $$.log
    TEST=$(cat $$.log |grep "optimized" |wc -l)
   ....
问题是我无法将输出写入新文件,我需要在不创建文件的情况下进行检查。
有没有办法将
ptipng-simulate-quiet$FILE
的输出分配到一个变量中,然后进行检查?

我从未使用过optipng,也不知道是否需要-log开关,但您可以像这样重写脚本:

if [ "${image}" = "*.png" ]; then
    local baseName="$(basename "{image})"    #I assume this code is executed inside a function, that's why I used local

    local output
    if ! output="$(optipng -simulate -quiet "${baseName})"; then     #I assume that optipng returns error code in case of failure. In such case, when errior occures, "if" logic will be executed
        printf "Failed to test file ${baseName}"
        return
    fi

    if ! printf '%s' "${output}" | grep -qi "optimized"; then   #i for case insensitive, q for quiet. I ommited the wc -l because I did not see any reason for counting
        printf "Not optimized"
    fi
fi
据你所知,你可以:

TEST=$(optipng -simulate -quiet "$FILE" - | grep "optimized" | wc -l)
# or just handle grep return valud
if optipng -simulate -quiet "$FILE" - | grep -q "optimized"; then
      echo "It is optimized"
fi

将文件名替换为
-
optipng
输出为标准输出。

是否
-log/dev/stdout
工作?@KamilCuk否:/Does
optipng-simulate-quiet$file-
工作<代码>选择-模拟-安静--当我尝试时,是的,它有效。