判断文件是否为bash中的PDF

编程入门 行业动态 更新时间:2024-10-26 22:27:17
本文介绍了判断文件是否为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

本文发布于:2023-10-25 14:29:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1527224.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   bash   PDF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!