合并pdf中文件夹中所有图像文件的脚本

Script to merge all image files from a folder inside a pdf

我经常需要在 pdf 中合并大量图像。自然地,我想稍微自动化一下。 这是我需要的东西:

我遇到了一个 solution,它的工作原理与我所需要的完全一样,但对于名称中包含白色-space 的文件,它会失败。

#!/bin/bash
convert `ls -v *.jpg *.jpeg *.JPG *.JPEG *.png *.PNG *.gif *.GIF *.tiff *.TIFF *.webp *.WEBP *.bmp *.BMP` compilation_images.pdf

在 shell 脚本编写方面,我是一个彻头彻尾的菜鸟,所以如果我没有看到一个简单的解决方案,我提前道歉,但我自己找不到任何解决方案。

感谢您的帮助。

Trying to parse ls is wrong,如您所知。假设您正在使用 GNU 版本的实用程序,正如 linux 标签通常暗示的那样,请尝试类似的操作:

#!/usr/bin/env bash

# Turn on case-insensitive globs and make ones that
# don't match anything expand to nothing
shopt -s nocaseglob nullglob

readarray -d '' -t images < <(printf "%s[=10=]" *.jpg *.jpeg *.png *.gif *.tiff *.webp *.bmp | sort -Vz)
convert "${images[@]}" compilation_images.pdf

它使用了用 nul 字符分隔文件名的常用技巧,以便在使用排序后的文件名填充 bash 数组时安全地处理空格。