Linux shell - 检查并设置二进制文件的最后一个字节

标签 linux command-line

我有一组要处理的文件。我无法控制文件的创建或删除。 但是我知道每个文件的最后一个字节总是 xFF 并且未被使用。

我想处理文件并修改最后一个字节,这样我就可以知道它们已被处理。我在 shell 中找不到任何单字节操作的具体示例。

伪代码

for file in *
do
if (lastbyteof($file) == xFF)
then
    # Process this file
    ...
    # Mark the file so we don't process it again
    lastbyteof($file) = xF0
fi

最佳答案

我把我的要求从最后一个字节改成了第四个字节,然后这样处理。 非常欢迎任何改进或替代建议。

for file in *.ts
do
    # Get the value of the fourth byte (skip the first 3)
    checkByte=`hexdump -n1 -s3 -e'"" 1/1 "%02xf"' "$file"`
    if [ $checkByte == "ff" ]
    then
        # Process this file

        # Change the fourth byte to 0xFE
        echo -ne \\xFE | dd conv=notrunc bs=1 count=1 seek=3 of="$file"
    fi
done

关于Linux shell - 检查并设置二进制文件的最后一个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15320246/

相关文章:

linux - 如何运行脚本取决于不同的停止时间

linux - net.nf_conntrack_max 和 net.netfilter.nf_conntrack_max 之间的区别

scala - 用于检查正在使用哪些内存选项的 SBT 命令

git - 我可以使用 SSH URL 检查 git 存储库是否存在吗?

node.js - Node [file.js] 与运行浏览器和 nodejs .editor 产生不同的结果

c++ - 从 Qt 应用程序启动外部程序

linux - PDF-API2无法执行

linux - gettimeofday 系统调用如何工作?

shell - 跨 session 永久清除 fish shell 历史记录

java - 让 JVM 根据需要增加内存需求,直到达到 VM 限制的大小?