Link to home
Start Free TrialLog in
Avatar of mabell
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....
Avatar of ct.smith
ct.smith

This sort of thing, if available, will be specific to the API you're using.  Most languages don't offer anything beyond opening a file as text or binary, which is decided at compile time.

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.
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mabell

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
oops, take care with
     file $1/$2|awk '{printf $3}'
it may produce fals positives, for example see:
     awk '($5 == "text"){print}' /etc/magic