file
file 用例主要用于测试file命令。
file命令介绍
file命令可以通过查看文件内容来找出特定类型的文件,在UNIX/ Linux系统中,文件类型并不是由文件扩展名来决定的(windows中却正是这么做的),file命令的目的是从多个文件中收集文件类型统计信息。
file --help
Usage: file [OPTION...] [FILE...]
Determine type of FILEs.
--help display this help and exit
-v, --version output version information and exit
-m, --magic-file LIST use LIST as a colon-separated list of magic
number files
-z, --uncompress try to look inside compressed files
-Z, --uncompress-noreport only print the contents of compressed files
-b, --brief do not prepend filenames to output lines
-c, --checking-printout print the parsed form of the magic file, use in
conjunction with -m to debug a new magic file
before installing it
-e, --exclude TEST exclude TEST from the list of test to be
performed for file. Valid tests are:
apptype, ascii, cdf, compress, elf, encoding,
soft, tar, text, tokens
-f, --files-from FILE read the filenames to be examined from FILE
-F, --separator STRING use string as separator instead of `:'
-i, --mime output MIME type strings (--mime-type and
--mime-encoding)
--apple output the Apple CREATOR/TYPE
--extension output a slash-separated list of extnsions
--mime-type output the MIME type
--mime-encoding output the MIME encoding
-k, --keep-going don't stop at the first match
-l, --list list magic strength
-L, --dereference follow symlinks (default)
-h, --no-dereference don't follow symlinks
-n, --no-buffer do not buffer output
-N, --no-pad do not pad output
-0, --print0 terminate filenames with ASCII NUL
-p, --preserve-date preserve access times on files
-P, --parameter set file engine parameter limits
indir 15 recursion limit for indirection
name 30 use limit for name/use magic
elf_notes 256 max ELF notes processed
elf_phnum 128 max ELF prog sections processed
elf_shnum 32768 max ELF sections processed
-r, --raw don't translate unprintable chars to \ooo
-s, --special-files treat special (block/char devices) files as
ordinary ones
-C, --compile compile file specified by -m
-d, --debug print debugging messages
必要参数
-b 文件名不显示
-c 显示详细处理信息
-L 操作对象为符号链接所指向的对象
-z 压缩文件的处理
-C 列出magic.mgc的输出
-n 将结果输出到标准输出
-f<文件名> 指定文件名
-m<文件名> 指定魔法数字名
选择参数
--help 显示帮助信息
-v 显示版本信息
实例:
1.查看所有文件类型 file * 2.2.查看具体某一文件的类型:file filename 3.-b参数,打印出不包含文件名的文件类型信息: 4.-z 参数,可以获取用gzip、zip压缩过的文件的类型 5.-n参数,将信息标准输出
言归正传,file_test.sh 脚本主要验证是否支持多种文件格式,比如tar tar.gz rpm c ascii elf等。
用例设置:
Test01:测试文件命令是否识别ASCII文本文件
1)将文本写入已知文件
2)使用'file'命令获取已知文件的类型
例如:文件xyz.txt
3)grep为关键字“ASCII文本”的输出
'file'命令
4)如果上述步骤成功,则将测试声明为PASS,否则为fail
Test02:测试文件命令是否能识别bash shell脚本
1)将一个小shell脚本写入一个已知文件
2)使用'file'命令获取已知文件的类型
例如:文件xyz.sh
3)从'file'命令的输出中Grep关键字“Bourne-Again shell脚本”
4)如果上述步骤成功,则将测试声明为PASS,否则为fail
Test03:测试文件命令是否能识别bash shell脚本
使用Korn shell脚本执行类似的测试(如Test02)
Test04:测试文件命令是否可以识别C shell脚本
使用C shell脚本执行类似的测试(如Test02)
Test05:测试文件命令是否可以识别C程序文本 使用test2类似方法测试c程序文本
Test06:测试文件命令是否可以识别ELF二进制可执行文件
1)使用readelf确定主机是大端还是小端,并分别为字符串“MSB”或“LSB”分配TEST_ARCH
2)将小C程序写入已知的“.c”文件
3)使用“cc” Ex:cc xyz xyz.c
4)使用file命令获取对象文件的类型
5)在file输出中,grep ELF .*-bit $TEST_ARCH executable, .* 字符。
6)如果上述命令成功,则将测试声明为PASS
Test07:测试文件命令是否可以识别tar文件
1)将文本写入三个不同的文件
2)使用“tar”命令存档文件
例如:tar -cf ...
3)使用“file”命令获取归档文件的类型
例如:文件xyz.tar
4)grep从上面的'file'命令的输出的中匹配字符串“tar”
5)声明测试为PASS,如果上面的步骤成功,否则声明测试为FAIL
Test08:测试文件命令是否可以tar zip文件
1)将文本写入三个不同的文件
2)使用“tar”命令存档文件
例如:tar -cf ...
3)使用'gzip'命令来压缩tar文件
例如:gzip -f xyz.tar
4)使用“file”命令获取归档文件的类型
例如:文件xyz.tar.gz
5)grep从上面的文件commnand字符串“gzip压缩数据,。*”
6)声明测试为PASS,如果上面的步骤成功,否则声明测试为FAIL
代码片段
测试file命令能识别出ASCII文本文件。
export TCID=file01
export TST_COUNT=1
$LTPBIN/tst_resm TINFO "TEST #1:file command recogizes ASCII text files" # log信息
cat > $LTPTMP/test_file.txt <<EOF
this is a text file
to test file command
EOF # 制作txt文本文件
##执行file命令并检查输出。
file $LTPTMP/test_file.txt > $LTPTMP/file.out 2>&1
if [ $? -eq 0 ] # 判断返回值是否为0,验证file命令执行是否成功
then
grep -q "ASCII text" $LTPTMP/file.out # 匹配输出类型是否整齐
if [ $? -eq 0 ]
then
$LTPBIN/test_resm TPASS "file: Recognised ASCII file correctly"
rm -f $LTPTMP/test_file.txt
else
$LTPBIN/tst_res TFAIL $LTPTMP/file.out \
"file: Failed to recognise ASCII file correctlyi. Reason:"
TFAILCNT=$(( $TFAILCNT+1 )) #error code+1
fi
else
$LTPBIN/tst_res TFAIL $LTPTMP/file.out \
"file: failed to recognize ASCII file correctly\t\t"
TFAILCNT=$(( $TFAILCNT+1 ))
fi
TEST #2
测试是否识别shell脚本
export TCID=file02
export TST_COUNT=2
$LTPBIN/tst_resm TINFO "TEST #2: file command recognizes bash shell scripts"
cat > $LTPTMP/bash_script.sh <<EOF
#! /bin/bash
echo "this is a shell script"
echo "used to test file command"
EOF
file $LTPTMP/bash_script.sh > $LTPTMP/file.out 2>&1
if [ $? -eq 0 ]
then
grep -q "Bourne-Again shell script" $LTPTMP/file.out
if [ $? -eq 0 ]
then
$LTPBIN/tst_resm TPASS "file: Recognised bash shell script correctly"
rm -f $LTPTMP/bash_script.sh
else
$LTPBIN/tst_res TFAIL $LTPTMP/file.out \
"file: Failed to recognise bash shell script. Reason"
TFAILCNT=$(( $TFAILCNT+1 ))
fi
else
$LTPBIN/tst_resm TFAIL "file: Failed to recognize bash shell script"
TFAILCNT=$(( $TFAILCNT+1 ))
fi
其他测试代码同上面类似,不再一一分析。