gpt4 book ai didi

bash - 在 bash 中判断文件是否为 PDF

转载 作者:行者123 更新时间:2023-12-05 08:17:38 26 4
gpt4 key购买 nike

我需要编写一个 bash 脚本来判断文件是否为 pdf 文件。但是,我不能简单地使用文件名或扩展名。

例如:

test.pdf.encrypt - 无法打开,因为文件本身已加密且文件属于计算机无法识别的未知类型。

test.pdf.decrypt - 即使扩展名为 .decrypt 也会打开

由于查看扩展名没有帮助,而且加密文件和解密文件的名称中间都有 .pdf,有没有办法让系统测试并查看文件是否可以用 pdf 阅读器读取?

我只需要可以输入到 bash 中的 if 语句中的命令。

if [this file is a working pdf file]; do
echo "$file is a working pdf file."
fi

最佳答案

另一种选择是在文件上使用 file:

type="$(file -b $file)"
if [ "${type%%,*}" == "PDF document" ]; then
echo "$file is a PDF file."
fi

关于bash - 在 bash 中判断文件是否为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16152583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com