我有一组要处理的文件。我无法控制文件的创建或删除。 但是我知道每个文件的最后一个字节总是 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/