判断文件是否为bash中的PDF [英] Tell if a file is PDF in bash

查看:58
本文介绍了判断文件是否为bash中的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to write a bash script to tell if a file is a pdf file. However, I cannot simply use the file name or extension.

例如:

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

test.pdf.encrypt - will not open as the file itself is encrypted and the file is of an unknown type the computer can't recognize.

test.pdf.decrypt-即使扩展名为.decrypt

test.pdf.decrypt - will open even though the extension is .decrypt

由于查看扩展名无济于事,并且加密和解密文件的名称中间都带有.pdf,是否有办法进行系统测试,并查看该文件是否甚至可以用pdf阅读器读取?

As looking at the extension does not help and both the encrypted and decrypted file have .pdf in the middle of the name, is there a way to have the system test and see if the file is even readable with a pdf reader?

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

I just need the command that I can input into an if statement in bash.

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

推荐答案

每个PDF文件均以%PDF 开头.您可以比较指定文件的前4个字符,以确保它是PDF.

Every PDF file starts with %PDF. You can compare the first 4 characters of a specified file in order to make sure it's a PDF.

FILE="/Users/Tim/Documents/My File.pdf"
if [ $(head -c 4 "$FILE") = "%PDF" ]; then
    echo "It's a PDF!"
fi

这篇关于判断文件是否为bash中的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆