我想根据字符串变量的内容在
sh
shell 中检测适当的文件扩展名。
我尝试使用该功能 file
- 手动
如果我这样做:
echo "bar" > foo.txt
file foo.txt
file --mime-type foo.txt
然后我得到一个有效的结果:
foo.txt: ASCII text
foo.txt: text/plain
但是如何在不创建文件并获取内容类型的情况下获得相同的结果?
我尝试过:
file <(echo 'bar')
并得到:
/dev/fd/63: symbolic link to pipe:[1452271]
并与:
echo 'bar' | file
我得到了:
Usage: file [-bcCdEhikLlNnprsSvzZ0] [--apple] [--extension] [--mime-encoding]
[--mime-type] [-e <testname>] [-F <separator>] [-f <namefile>]
[-m <magicfiles>] [-P <parameter=value>] [--exclude-quiet]
<file> ...
file -C [-m <magicfiles>]
file [--help]
有什么办法可以和
file
:
foo.txt: ASCII text
foo.txt: text/plain
但不创建文件本身?
也许我可以使用其他应用程序代替
file
?
尝试使用
file
的破折号“-”参数。
示例1:
file - <<< abc
/dev/stdin: ASCII text
示例2:
echo abc | file -
/dev/stdin: ASCII text
示例3:
file - <<< '#!/bin/sh'
/dev/stdin: POSIX shell script, ASCII text executable
示例4:
MY_VAR='#!/bin/awk'
file --mime-type - <<< $MY_VAR
/dev/stdin: text/x-awk