我需要编写一个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
发布评论