繁体   English   中英

如何使用for命令循环使用大小写字母的多个扩展名

[英]How to loop multiple extensions with upper and lower case letters using for command

提示:本站为国内最大中英文翻译问答网站,提供中英文对照查看,鼠标放在中文字句上可显示英文原文

我正在尝试在一个文件夹中循环一组 jpg 图像。 问题是有些图像的扩展名全是小写字母 (.jpg),而其他图像的扩展名全是大写字母 (.JPG)。

随着循环的进行,我正在尝试使用变量修饰符来更改扩展名。

我想出了如何在 LIST 部分中用 for var 来表达,但我无法使用修饰符来正确替换扩展名。

本质上,我需要将 jpg 文件转换为扩展名为 .mpc 和 .cache 的文件。

当我需要文件为FILE.mpcFILE.cache时,我最终得到名称类似于FILE.jpg.mpcFILE.JPG.jpg的文件

这是我的 function,如果您只for i in *.jpg它会很好用,但是当您for i in *.{jpg,JPG}一切都会崩溃。

这又是我到目前为止所拥有的。

imow()
{

    clear

    local i DIMENSIONS RANDOM

    # find all jpg files and create temporary cache files from them
    for i in *.{jpg,JPG}
    do
        # create random direcotories in case you are running this function more than once at the same time. it prevents cross-over.
        RANDOM="$(mktemp --directory)"
        "${RANDOM}" 2>/dev/null
        echo -e "\\nCreating two temporary cache files: ${RANDOM}/${i%%.jpg,JPG}.mpc + ${RANDOM}/${i%%.{jpg,JPG}}.cache\\n"
        DIMENSIONS="$(identify -format '%wx%h' "${i}")"
        convert "${i}" -monitor -filter 'Triangle' -define filter:support='2' -thumbnail "${DIMENSIONS}" -strip \
        -unsharp '0.25x0.08+8.3+0.045' -dither None -posterize '136' -quality '82' -define jpeg:fancy-upsampling='off' \
        -define png:compression-filter='5' -define png:compression-level='9' -define png:compression-strategy='1' \
        -define png:exclude-chunk='all' -auto-level -enhance -interlace 'none' -colorspace 'sRGB' "${RANDOM}/${i%%.{jpg,JPG}}.mpc"
        clear
        for i in "${RANDOM}"/*.mpc
        do
            if [ -f "${i}" ]; then
                echo -e "\\nOverwriting original file with optimized self: ${i} >> ${i%%.mpc}.jpg\\n"
                convert "${i}" -monitor "${i%%.mpc}.jpg"
                if [ -f "${i%%.mpc}.jpg" ]; then
                    mv "${i%%.mpc}.jpg" "${PWD}"
                    rm -fr "${RANDOM}"
                    clear
                else
                    clear
                    echo 'Error: Unable to find the optimized image and therefore can'\''t overwrite the original.'
                    echo
                    exit 1
                fi
            fi
        done
    done
}

就像我提到的那样,如果您只使用 .jpg 扩展名运行它,效果会很好。 这是一个工作示例。

imow()
{

    clear

    local i DIMENSIONS RANDOM

    # find all jpg files and create temporary cache files from them
    for i in *.jpg
    do
        # create random directories in case you are running this function more than once at the same time. it prevents cross-over.
        RANDOM="$(mktemp --directory)"
        "${RANDOM}" 2>/dev/null
        echo -e "\\nCreating two temporary cache files: ${RANDOM}/${i%%.jpg}.mpc + ${RANDOM}/${i%%.jpg}.cache\\n"
        DIMENSIONS="$(identify -format '%wx%h' "${i}")"
        convert "${i}" -monitor -filter 'Triangle' -define filter:support='2' -thumbnail "${DIMENSIONS}" -strip \
        -unsharp '0.25x0.08+8.3+0.045' -dither None -posterize '136' -quality '82' -define jpeg:fancy-upsampling='off' \
        -define png:compression-filter='5' -define png:compression-level='9' -define png:compression-strategy='1' \
        -define png:exclude-chunk='all' -auto-level -enhance -interlace 'none' -colorspace 'sRGB' "${RANDOM}/${i%%.jpg}.mpc"
        clear
        for i in "${RANDOM}"/*.mpc
        do
            if [ -f "${i}" ]; then
                echo -e "\\nOverwriting original file with optimized self: ${i} >> ${i%%.mpc}.jpg\\n"
                convert "${i}" -monitor "${i%%.mpc}.jpg"
                if [ -f "${i%%.mpc}.jpg" ]; then
                    mv "${i%%.mpc}.jpg" "${PWD}"
                    rm -fr "${RANDOM}"
                    clear
                else
                    clear
                    echo 'Error: Unable to find the optimized image and therefore can'\''t overwrite the original.'
                    echo
                    exit 1
                fi
            fi
        done
    done

${i%%.jpg,JPG}${i%%.{jpg,JPG}}不做你想做的事。 第一个尝试删除确切的后缀.jpg,JPG ,第二个尝试删除确切的后缀.{jpg,JPG} 由于在代码中的那个位置后缀保证为.jpg.JPG ,因此${i%.*}是一个安全的替代方案。

当我读到它时,您的 function 已经覆盖了原始的.jpg文件,但是硬编码了一个小写的.jpg文件,这使得旧的.JPG文件到处都是。 我只是将转换形式化为小写,除非有一些特定的原因你不应该/不能。

摆脱那些损坏的大写变量,以及使用与外循环相同的循环变量的内循环,等等。我移动了一些东西,但没有安装 imagemagick,所以这是徒手编辑; 我相信有人会指出任何明显的逻辑错误,因为我无法真正按照自己喜欢的方式对其进行测试。

我显式测试了imagemagick 返回代码以简化一点,并添加了一个测试以确保mv成功。 如果原始文件的扩展名不是全部小写,则在暂存优化文件后应将其删除。

我还删除了正在擦除 output 的clear命令,您刚刚明确登录支持将convert垃圾邮件发送到被擦除的日志,除非有问题。

imow() {
  local orig mpc new dim rc tdir boilerplate
  tdir="$(mktemp --directory)" 
  boilerplate=( -monitor -filter 'Triangle' -define filter:support='2' 
    -strip -unsharp '0.25x0.08+8.3+0.045' -dither None -posterize '136'
    -quality '82' -define jpeg:fancy-upsampling='off' 
    -define png:compression-filter='5' -define png:compression-level='9'
    -define png:compression-strategy='1' -define png:exclude-chunk='all'
    -auto-level -enhance -interlace 'none' -colorspace 'sRGB' )
  for orig in *.[Jj][Pp][Gg]
  do dim="$(identify -format '%wx%h' "$orig")"
     if convert "$orig" "${boilerplate[@]}" -thumbnail "$dim" "${tdir}/${orig%.???}.mpc" > "$tdir/convert-mpc.out"
     then for mpc in "${tdir}"/*.mpc
          do new="${mpc%.mpc}.jpg"
             if convert "$mpc" -monitor "$new" > "$tdir/convert-jpg.out"
             then echo "Replacing $orig with optimized version $new"
                  if mv "$new" "$PWD"
                  then [[ "$orig" == "$new" ]] || rm -f "$orig"
                       rm -fr "${tdir:?safety check}/*.*" # can't expand to rm -fr /*
                  else rc=$?;
                       echo "Cannot mv '$new' to '$PWD': error $rc; aborting" >&2
                       exit $rc
                  fi
        else rc=$?
                  echo "convert error $rc on $mpc; c.f. $tdir - aborting." >&2
                  exit $rc
             fi
          done
     else rc=$?
          echo "convert error $rc on $orig; c.f. $tdir - aborting." >&2
          exit $rc
     fi
  done
  rm -fr "$tdir"
}

*.[Jj][Pp][Gg]匹配扩展名上/下的任意组合。

确保您测试和修改以满足您的需要。
祝你好运。

这对你有用吗?

shopt -s nullglob
cd
cd desktop/tests
for i in *.jpg *.JPG; do
echo $i
done
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM