unix文件实用程序:魔术语法

时间:2018-01-20 14:25:04

标签: file unix mime

我想为magic实用程序创建自定义file文件,但我很难理解man magic中描述的语法。

我需要测试几个地方,每个地方都可以包含几个字符串。只有所有测试成功,才会打印文件类型。

总结一下,如果它是SQL数据库中的字段,我想要一个类似的测试:

( byte_0 = "A" OR byte_0 = "B" OR byte_0 = "C" )
AND
( byte_1_to_3 = "DEF" OR byte_1_to_3 = "GHI" OR byte_1_to_3 = "JKL" )

或者用Perl regexp语法:

m/^
  [ABC]
  (DEF|GHI|JKL)
/x

1 个答案:

答案 0 :(得分:1)

file有自己的语法,有数百个例子。如果文档不清楚,您应该首先阅读接近预期更改的示例。这就是我在ncurses中所做的,例如在terminfo magic-file中,将Solaris xcurses标头描述为一系列字符串:

    # Rather than SVr4, Solaris "xcurses" writes this header:
    0   regex       \^MAX=[0-9]+,[0-9]+$
    >1  regex       \^BEG=[0-9]+,[0-9]+$
    >2  regex       \^SCROLL=[0-9]+,[0-9]+$
    >3  regex       \^VMIN=[0-9]+$
    >4  regex       \^VTIME=[0-9]+$
    >5  regex       \^FLAGS=0x[[:xdigit:]]+$
    >6  regex       \^FG=[0-9],[0-9]+$
    >7  regex       \^BG=[0-9]+,[0-9]+, Solaris xcurses screen image
    #

但是没有通过阅读这个例子获得的洞察力,

    0   string      \032\001
    # 5th character of terminal name list, but not Targa image pixel size (15 16 24 32)
    >16 ubyte       >32
    # namelist, if more than 1 separated by "|" like "st|stterm| simpleterm 0.4.1"
    >>12 regex \^[a-zA-Z0-9][a-zA-Z0-9.][^|]* Compiled terminfo entry "%-s"

手册页没有(正如您所报告的那样)足够明确file按顺序处理一系列编号的步骤。