Solved

testing file types with if statement

Posted on 2002-03-16
4
151 Views
Last Modified: 2010-04-22
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....
0
Comment
Question by:mabell
  • 2
4 Comments
 
LVL 4

Expert Comment

by:ct.smith
ID: 6873561
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.
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 6873984
man test will give you all the answers, the usage of this test depends on your shell (mainly csh vs. sh variants), for example to test a pipe:

   if [ -p file ] then echo is PIPE

If you menan something like "content is word .doc" format vs. "content is plain ASCII" format, use the file command: man file
(keep in mind that the result of the file command highly depends on the quality of your /etc/magic file.
0
 

Author Comment

by:mabell
ID: 6876967
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 6877950
oops, take care with
     file $1/$2|awk '{printf $3}'
it may produce fals positives, for example see:
     awk '($5 == "text"){print}' /etc/magic
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now