mabell
asked on
testing file types with if statement
Hi all, I want to know how to test if a file is a text file or any other type in a "if" statement (In a shell script) ?
ex: if [ test.wkr is text ] then do....
ex: if [ test.wkr is text ] then do....
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok thx :)
I do this (to test if a file is a text file) :
fileTest=$(file $1/$2|awk '{printf $3}')
if [ $fileTest = "text," ]
then
...
were $1 is a directory and $2 is a file passed in parameters
I do this (to test if a file is a text file) :
fileTest=$(file $1/$2|awk '{printf $3}')
if [ $fileTest = "text," ]
then
...
were $1 is a directory and $2 is a file passed in parameters
oops, take care with
file $1/$2|awk '{printf $3}'
it may produce fals positives, for example see:
awk '($5 == "text"){print}' /etc/magic
file $1/$2|awk '{printf $3}'
it may produce fals positives, for example see:
awk '($5 == "text"){print}' /etc/magic
Anyways, you migth have some luck if you look for references to handling or discovering mime types in whatever API/library/language you're using.