I would like to detect an appropriate file extension in sh
shell based on a content of a string variable.
I tried to use for that file
- manual
If I do:
echo "bar" > foo.txt
file foo.txt
file --mime-type foo.txt
then I get a valid result:
foo.txt: ASCII text
foo.txt: text/plain
but how to get the same result without creating a file and get content type?
I tried:
file <(echo 'bar')
and got:
/dev/fd/63: symbolic link to pipe:[1452271]
and with:
echo 'bar' | file
I got:
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]
Is there any way to get with file
:
foo.txt: ASCII text
foo.txt: text/plain
but without creating the file itself?
Perhaps I could use other app instead of file
?
Try to use dash "-" argument of file
.
Example 1:
file - <<< abc
/dev/stdin: ASCII text
Example 2:
echo abc | file -
/dev/stdin: ASCII text
Example 3:
file - <<< '#!/bin/sh'
/dev/stdin: POSIX shell script, ASCII text executable
Example 4:
MY_VAR='#!/bin/awk'
file --mime-type - <<< $MY_VAR
/dev/stdin: text/x-awk